Wpf binding custom converter
This is the code found in the Windows .xaml file:
And this is the code for the 'NamesConverter':
I am guessing that the reason that this is not working is because it is bound to the ObservableCollection, and will be only updated when the actual ObservableCollection is changed, not the items inside of it.
- Trigger an update manually. You can do this by using BindingOperations.GetBindingExpression(window, Window1.NamesProperty).UpdateTarget().
- Use a multi-binding. Slightly more complicated, but is transparent to the code that's updating the collection. To accompilsh that:
- Use a MultiBinding on the TextBlock.Text property, like so:
Hope this helps,
- Marked as answer by dustamulet Tuesday, July 01, 2008 10:32 PM
All replies
- Trigger an update manually. You can do this by using BindingOperations.GetBindingExpression(window, Window1.NamesProperty).UpdateTarget().
- Use a multi-binding. Slightly more complicated, but is transparent to the code that's updating the collection. To accompilsh that:
- Use a MultiBinding on the TextBlock.Text property, like so:
Hope this helps,
- Marked as answer by dustamulet Tuesday, July 01, 2008 10:32 PM
Names = new ObservableCollection < string >();
Also I don't think you need a DependencyProperty here, a simple ReadOnly Property of Window1 Class will work.
Wisdom is an art to know, what to ignore.
- Proposed as answer by Harish Suhanda Tuesday, July 01, 2008 10:09 AM
yourcorrection.CollectionChanged +=new System.Collections.Specialized.NotifyCollectionChangedEventHandler(OnCollectionChanged);
All you need to do is make a simple changes in your Window1 Class Constructor.
(Note that the first method I suggested will not work unless Names is a DP.)
To update the TextBlock automatically, you need to altogether assign a new ObservableCollection<String> to the Names property of Window1. That is only how the INotification change will recognize a change in the Names Dependency Property and will update the source.
Wisdom is an art to know, what to ignore.
Thanks you guys are very helpful - explaining why it works helps even more :)
Microsoft is conducting an online survey to understand your opinion of the Msdn Web site. If you choose to participate, the online survey will be presented to you when you leave the Msdn Web site.