我在背景和类中ViewModel
(已实现)只有一个类型的属性。我的 ComboBox SelectedItem 绑定到类别的实例。当我更改实例的值时,未更新 SelectedItem 并且未更改 Combobox。INotifyPropertyChanged
Category
string
编辑:代码
组合框:
<ComboBox x:Name="categoryComboBox" Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="2"
Margin="10" ItemsSource="{Binding Categories}"
DisplayMemberPath="Name" SelectedValue="{Binding NodeCategory, Mode=TwoWay}"/>
财产:
private Category _NodeCategory;
public Category NodeCategory
{
get
{
return _NodeCategory;
}
set
{
_NodeCategory = value;
OnPropertyChanged("NodeCategory");
}
}
[Serializable]
public class Category : INotifyPropertyChanged
{
private string _Name;
[XmlAttribute("Name")]
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
[field:NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
}
我正在尝试的是:当我设置
NodeCategory = some_list_of_other_objects.Category;
Combobox
以适当的方式选择该项目DisplayMemberPath