我有一個組合框在我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