I am currently writing my own dictionary that implements INotifyPropertyChanged . See below:
public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } new public Item this[TKey key] { get { return base.Item[key]; } set(TValue value) { if (base.Item[key] != value) { base.Item[key] = value; OnPropertyChanged("XXX");
My goal is simple: when a certain meaning in a dictionary has changed, let it know that it has changed. All WPF elements that are bound to the corresponding key name must then be updated.
Now my question is: which line should I use as propertyName to notify?
I tried "[" + key.ToString() + "]" , "Item[" + key.ToString() + "]" and just key.ToString() . All of them did not seem to work because WPF elements were not updated.
Using String.Empty ( "" ) updates the WPF elements, but I do not use it because it will update all WPF elements that are connected by the same dictionary, even if they have different keys.
This is how my binding looks in XAML:
<TextBlock DataContext="{Binding Dictionary}" Text="{Binding [Index]}" />
Index is, of course, the key name in my dictionary.
Instead of using INotifyPropertyChanged, some suggested using INotifyCollectionChanged . I tried this:
Dim index As Integer = MyBase.Keys.ToList().IndexOf(key) Dim changedItem As Object = MyBase.ToList().ElementAt(index) RaiseEvent CollectionChanged(Me, New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, changedItem, index))
But it also does not update related WPF elements.