簡體   English   中英

將DataContext窗口綁定到ViewModel

[英]Binding DataContext Window to ViewModel

好的,我嘗試了幾種方法,但是沒有一種方法可以解決我的問題。 我有一個帶有單個ComboBox的簡單窗口。 我將代碼更改為MVVM,因此現在所有內容仍在Code-Behind中,應轉到ViewModel等。
但是,即使是第一步(將ViewModel綁定到視圖/窗口),我似乎也無法將它們綁定在一起。

我的窗口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>

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));
        }
    }
}

我收到以下錯誤消息:名稱空間“ clr-namespace:CustomerGuidance.ViewModels”中沒有名稱“ EmployeeViewModel”。
現在的問題是:我想念什么? 如何將ViewModel綁定到我的window-XAML?

您應該構建代碼以使錯誤消失。
這是因為名稱空間在設計人員構建之前依賴於(您的程序)的程序集中尚不可用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM