使用MVVM绑定ComboBox SelectedItem

我的ComboBox中SelectedItem有问题。

 

这没什么好的 如果我打开ComboBox,我会看到值。

在此处输入图像描述 如果我选择一个项目,则不会显示所选项目。

有人有想法吗?

在我的ViewModel中,我有以下两个属性:

 public ObservableCollection SalesPeriods { get; private set; } private SalesPeriodVM selectedSalesPeriod; public SalesPeriodVM SelectedSalesPeriod { get { return selectedSalesPeriod; } set { if (selectedSalesPeriod != value) { selectedSalesPeriod = value; RaisePropertyChanged("SelectedSalesPeriod"); } } } 

这些是该类的一些属性:

 public SalesPeriodVO Vo { get { return period; } } public int Year { get { return period.Year; } set { if (period.Year != value) { period.Year = value; RaisePropertyChanged("Year"); } } } public int Month { get { return period.Month; } set { if (period.Month != value) { period.Month = value; RaisePropertyChanged("Month"); } } } public string displayPeriod { get { return this.ToString(); } } public override string ToString() { return String.Format("{0:D2}.{1}", Month, Year); } 

编辑 :以下发生如果我删除属性DisplayMemberPath: 在此处输入图像描述

您似乎在您的ComboBox上不必要地设置属性。 您可以删除具有不同用途的DisplayMemberPathSelectedValuePath属性。 您可以在此处查看SelectedItem,SelectedValue和SelectedValuePath之间的差异,以获取对这些属性的解释。 试试这个:

  

此外,使用displayPeriod属性毫无意义,因为WPF Framework会自动为需要显示的对象调用ToString方法,而这些对象没有明确地为它们设置DataTemplate


更新>>>

由于我看不到你的所有代码,我无法告诉你你做错了什么。 相反,我所能做的就是为您提供如何实现您想要的完整工作示例。 我从你的课程中删除了无意义的displayPeriod属性以及你的SalesPeriodVO属性,因为我一无所知……也许这就是你问题的原因?? 试试这个:

 public class SalesPeriodV { private int month, year; public int Year { get { return year; } set { if (year != value) { year = value; NotifyPropertyChanged("Year"); } } } public int Month { get { return month; } set { if (month != value) { month = value; NotifyPropertyChanged("Month"); } } } public override string ToString() { return String.Format("{0:D2}.{1}", Month, Year); } public virtual event PropertyChangedEventHandler PropertyChanged; protected virtual void NotifyPropertyChanged(params string[] propertyNames) { if (PropertyChanged != null) { foreach (string propertyName in propertyNames) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged(this, new PropertyChangedEventArgs("HasError")); } } } 

然后我在视图模型中添加了两个属性:

 private ObservableCollection salesPeriods = new ObservableCollection(); public ObservableCollection SalesPeriods { get { return salesPeriods; } set { salesPeriods = value; NotifyPropertyChanged("SalesPeriods"); } } private SalesPeriodV selectedItem = new SalesPeriodV(); public SalesPeriodV SelectedItem { get { return selectedItem; } set { selectedItem = value; NotifyPropertyChanged("SelectedItem"); } } 

然后使用您的值初始化集合:

 SalesPeriods.Add(new SalesPeriodV() { Month = 3, Year = 2013 } ); SalesPeriods.Add(new SalesPeriodV() { Month = 4, Year = 2013 } ); 

然后数据将这两个属性绑定到ComboBox

  

就是这样……这就是一个完美的例子所需要的一切。 您应该看到项目的显示来自ToString方法而没有您的displayPeriod属性。 希望您可以通过此代码示例解决您的错误。

      //student Class public class Student { public int Id { set; get; } public string name { set; get; } } //set 2 properties in MainWindow.xaml.cs Class public ObservableCollection studentInfo { set; get; } public Student SelectedstudentInfo { set; get; } //MainWindow.xaml.cs Constructor public MainWindow() { InitializeComponent(); bindCombo(); this.DataContext = this; cmbData.ItemsSource = studentInfo; } //method to bind cobobox or you can fetch data from database in MainWindow.xaml.cs public void bindCombo() { ObservableCollection studentList = new ObservableCollection(); studentList.Add(new Student { Id=0 ,name="==Select=="}); studentList.Add(new Student { Id = 1, name = "zoyeb" }); studentList.Add(new Student { Id = 2, name = "siddiq" }); studentList.Add(new Student { Id = 3, name = "James" }); studentInfo=studentList; } //button click to get selected student MainWindow.xaml.cs private void Button_Click(object sender, RoutedEventArgs e) { Student student = SelectedstudentInfo; if(student.Id ==0) { MessageBox.Show("select name from dropdown"); } else { MessageBox.Show("Name :"+student.name + "Id :"+student.Id); } }