简体   繁体   English

将DataContext窗口绑定到ViewModel

[英]Binding DataContext Window to ViewModel

Ok, I tried it several ways, but none worked as it should be in my case. 好的,我尝试了几种方法,但是没有一种方法可以解决我的问题。 I have a simple Window with a single ComboBox. 我有一个带有单个ComboBox的简单窗口。 I am changing the code to MVVM, so now everything is still in the Code-Behind and should go to a ViewModel, etc. 我将代码更改为MVVM,因此现在所有内容仍在Code-Behind中,应转到ViewModel等。
But even on the first step (binding the ViewModel to the View/Window) I don't seem to be able to bind them together. 但是,即使是第一步(将ViewModel绑定到视图/窗口),我似乎也无法将它们绑定在一起。

My Window XAML: 我的窗口XAML:

<Window x:Class="CustomerGuidance.ClientWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:VM="clr-namespace:CustomerGuidance.ViewModels"
    Title="Stop'n'Go - Client" Height="22" Width="229"
    Loaded="ClientWindow_OnLoaded" WindowStyle="None" 
    WindowStartupLocation="Manual" Top="0" Left="0"
    ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True">
<Window.DataContext>
    <VM:EmployeeViewModel />
</Window.DataContext>
<Canvas Background="Gainsboro">
    <ComboBox Name="EmployeesComboBox"
              ItemsSource="{Binding EmployeeEntries}"
              Width="192" FontFamily="Arial" FontSize="14">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Lastname}" />
                    <TextBlock Text=", " />
                    <TextBlock Text="{Binding Surname}" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</Canvas>

The ViewModel looks like this: ViewModel看起来像这样:

using System.Collections.ObjectModel;
using System.ComponentModel;

namespace CustomerGuidance.ViewModels
{
    public class EmployeeViewModel : INotifyPropertyChanged
    {
        public EmployeeViewModel()
        {
        }

        public static ObservableCollection<ServerWindow.EmployeeEntry> EmployeeEntries { get; set; } = new ObservableCollection<ServerWindow.EmployeeEntry>();

        private string _surname;
        private string _lastname;
        private int _id;

        public string Surname
        {
            get { return _surname; }
            set
            {
                if (_surname == value)
                    return;
                _surname = value;
                NotifyPropertyChanged("Surname");
            }
        }

        public string Lastname
        {
            get { return _lastname; }
            set
            {
                if (_lastname == value)
                    return;
                _lastname = value;
                NotifyPropertyChanged("Lastname");
            }
        }

        public int Id
        {
            get { return _id; }
            set
            {
                if (_id == value)
                    return;
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }

        public virtual event PropertyChangedEventHandler PropertyChanged;

        protected virtual void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

I get the following error message: "The Name "EmployeeViewModel" is not available in the namespace "clr-namespace:CustomerGuidance.ViewModels". 我收到以下错误消息:名称空间“ clr-namespace:CustomerGuidance.ViewModels”中没有名称“ EmployeeViewModel”。
And now the question: What am I missing? 现在的问题是:我想念什么? How can I bind the ViewModel to my window-XAML? 如何将ViewModel绑定到我的window-XAML?

You should build your code for the errors to disappear. 您应该构建代码以使错误消失。
It's because the namespace is not yet available in the assembly the designer relies on (your program) before it has been built. 这是因为名称空间在设计人员构建之前依赖于(您的程序)的程序集中尚不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM