我有一个组合框在我MainWindow.xaml文件,像这样的工作:INotifyPropertyChanged的不使用WPF数据绑定
<ComboBox Name="material1ComboBox"
HorizontalAlignment="Center"
MinWidth="100"
ItemsSource="{Binding ViewProperties.MaterialDropDownValues}"
SelectedValue="{Binding ViewProperties.Material1SelectedValue}"
SelectionChanged="Material1ComboBoxSelectionChanged">
</ComboBox>
我分配使用this.datacontext = this
在代码隐藏在DataContext。
我创建了作为MainWindow
属性进行访问,并且是实现INotifyPropertyChanged并包含MaterialDropDownValues
作为属性类ViewProperties
。
我甚至将MaterialDropDownValues
更改为ObservableCollection
。
问题是数据绑定工作在初始化但是如果MaterialDropDownValues
属性更改组合框值不更新。
我在ViewProperties
类以下内容:
public ObservableCollection<string> MaterialDropDownValues
{
get { return this.materialDropDownValues; }
set
{
this.materialDropDownValues = value;
OnPropertyChanged("MaterialDropDownValues");
}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
任何想法,为什么这是不工作?我可以找到的所有其他答案建议实施INotifyPropertyChanged并使该属性成为可观察集合。
请注意,如果您只是从列表中添加/删除数据,则不应重新绑定整个集合。你应该这样做:'materialDropDownValues.Remove(value);'。 –
在过去,当我遇到这个问题时,它有时是由其他(代码隐藏)代码直接编程设置ItemsSource(从而终止绑定)导致的。 – bigtlb
注意:整个列表可以立即更改,不仅仅是需要添加或删除的单个项目。 – Seth