I've got a ListBox and an ObservableCollection<HostEntry>. HostEntry implements INotifyPropertyChanged. At present, I'm binding them this way:
lstHosts.DataContext = _hosts;
lstHosts.DisplayMemberPath = "HostName";
Which works great. When I edit one of the HostEntries' name, the ListBox gets refreshed automatically, showing the new name.
However, I'd rather have it display the HostEntry.ToString() as it does by default (not setting DiplayMemberPath, but if I do this, the list doesn't get refreshed. I believe this is because the HostName property fires a PropertyChanged event, but there's nothing to signal that ToString() has changed.
Is there perhaps something I can add to this method:
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
To inform the ListBox that it should refresh this item?
I don't like the idea of calling ListBox.Items.Refresh() explicitly... what if I miss a case, or whatever there are multiple views of this list? It also shouldn't be baked into the collection, because what if I want to use different collections of HostEntry? Should be a way to do it somewhere inside the HostEntry class, no?
Edit: I want to use ToString() because I don't simply want to display the HostName property. I want to do some string formatting with some other properties, which I can easily do in ToString().