简体   繁体   中英

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. I am changing the code to MVVM, so now everything is still in the Code-Behind and should go to a ViewModel, etc.
But even on the first step (binding the ViewModel to the View/Window) I don't seem to be able to bind them together.

My Window 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:

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".
And now the question: What am I missing? How can I bind the ViewModel to my 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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