【问题标题】:WPF ComboBox Binding : MVVMWPF 组合框绑定:MVVM
【发布时间】:2015-09-15 01:41:58
【问题描述】:

我正在尝试使用 MVVM 设计模式将 WPF 组合框 ItemsSource 与集合绑定。以下是我的代码

XAML:

<ComboBox Height="30" Width="200" ItemsSource="{Binding PeopleList,Mode=TwoWay}"></ComboBox>
<TextBlock Height="Auto" Width="Auto" Text="{Binding SelectedPerson.ContactNo}"></TextBlock>

后面的代码:

public MainWindow()
        {
            InitializeComponent();
            ViewModel vm = new ViewModel();
            DataContext = vm;
        }

模型类:

class People : INotifyPropertyChanged
    {
        private string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        }

        private string contactNo;

        public string ContactNo
        {
            get { return contactNo; }
            set
            {
                contactNo = value;
                NotifyPropertyChanged("ContactNo");
            }

        }

        private ObservableCollection<People> peopleList;

        public ObservableCollection<People> PeopleList
        {
            get { return peopleList; }
            set
            {
                peopleList = value;
                NotifyPropertyChanged("PeopleList");
            }
        }

        private People selectedPerson;

        public People SelectedPerson
        {
            get { return selectedPerson; }
            set
            {
                selectedPerson = value;
                NotifyPropertyChanged("SelectedPerson");
            }
        }




        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

ViewModel 类:

class ViewModel
           {
        ObservableCollection<People> PeopleList = null;
        public ViewModel()
        {
            PeopleList = new ObservableCollection<People>();
            People p1 = new People { Name = "Naresh", ContactNo = "9574733355" };
            People p2 = new People { Name = "Ritesh", ContactNo = "9099028779" };
            People p3 = new People { Name = "Saumya", ContactNo = "9904848779" };

            PeopleList.Add(p1);
            PeopleList.Add(p2);
            PeopleList.Add(p3);

            People People = new People();
            People.PeopleList = PeopleList;

        }

所以,这就是我到目前为止所做的。在这里,我面临的问题是当我单击组合框时没有任何反应。

提前感谢您的帮助。

【问题讨论】:

  • 输出窗口中是否有任何绑定错误?

标签: wpf mvvm data-binding combobox itemsource


【解决方案1】:

PeopleList 必须是不动产(不是本地字段):

public ObservableCollection<People> PeopleList { get; set; }

【讨论】:

【解决方案2】:

您必须替换字段条目

ObservableCollection<People> PeopleList = null;

通过属性条目

public ObservableCollection<People> PeopleList { get; set; }

只能绑定属性,不能绑定字段。

此外,您应该将 DisplayMemberPath 设置为显示比类型名称“People”更多的内容。这三个项目无法区分。你可以显示人的名字

        <ComboBox Height="30" Width="200" ItemsSource="{Binding PeopleList,Mode=TwoWay}" DisplayMemberPath="Name"></ComboBox>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-11
    • 2015-07-11
    • 2015-04-27
    • 2013-06-20
    • 2011-06-13
    • 2023-03-18
    • 2018-06-12
    • 2015-08-20
    相关资源
    最近更新 更多