At the moment I'm 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"); // what string should I use?
}
}
}
My goal is simple: when a certain value in the Dictionary has changed, notify that it has changed. All WPF elements that have a binding with the corresponding key name should then update themselves.
Now my question is: what string should I use as propertyName
in order to notify?
I have tried "[" + key.ToString() + "]"
, "Item[" + key.ToString() + "]"
and simply key.ToString()
. They all did not seem to work, because the WPF elements did not update.
Using String.Empty
(""
) does update the WPF elements, but I'm not using this, because this will update all WPF elements that are bound the same dictionary, even though they have different keys.
This is how my binding looks like in XAML:
<TextBlock DataContext="{Binding Dictionary}" Text="{Binding [Index]}" />
Index
is of course the name of the key in my dictionary.
In stead of using INotifyPropertyChanged, some have suggested to use 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 that does not update the bound WPF elements either.