0

我正在尝试使用 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));
        }

视图模型类:

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;

        }

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

提前感谢您的帮助。

4

2 回答 2

2

PeopleList需要是不动产(不是本地字段):

public ObservableCollection<People> PeopleList { get; set; }
于 2015-06-28T15:37:25.660 回答
0

您必须替换字段条目

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>
于 2017-07-31T22:58:29.100 回答