How to force- invalidate the DataContext of a control ?
-
Basically, I have a TreeView and a ListView. When users changes the selection of TreeNode then, I updated the Listview with some relevant data to that selected TreeNode. But I am using a converter here to populate the data for the ListView. The binding xaml of the ListView is as follows <listbox x:name="datagrid" datacontext="{Binding Path=SelectedValue, Converter={StaticResource DocumentConverter}, ElementName=myTree}" xmlns:x="#unknown"></listbox> Now, I have a button at some where into the same window, and I want when user will click onto that button, ListBox data should be refreshed. Basically the logic of the converter does something different when a button has been clicked. So, if somehow i was able to re-execute the converter code that would be great. In short, I need to invalidate the DataContext of the ListBox when the button gets clicked. How can i do that? Thanks in advance!
Moim Hossain Senior Software Engineer KAZ Software
-
Basically, I have a TreeView and a ListView. When users changes the selection of TreeNode then, I updated the Listview with some relevant data to that selected TreeNode. But I am using a converter here to populate the data for the ListView. The binding xaml of the ListView is as follows <listbox x:name="datagrid" datacontext="{Binding Path=SelectedValue, Converter={StaticResource DocumentConverter}, ElementName=myTree}" xmlns:x="#unknown"></listbox> Now, I have a button at some where into the same window, and I want when user will click onto that button, ListBox data should be refreshed. Basically the logic of the converter does something different when a button has been clicked. So, if somehow i was able to re-execute the converter code that would be great. In short, I need to invalidate the DataContext of the ListBox when the button gets clicked. How can i do that? Thanks in advance!
Moim Hossain Senior Software Engineer KAZ Software
Hi, The quick and dirty way is to set the DataContext to null then set it back to its original source. This should force an update. Colin E.
-
Hi, The quick and dirty way is to set the DataContext to null then set it back to its original source. This should force an update. Colin E.
You could also try using the
BindingExpression
class in your button's click event handler:BindingExpression expression = BindingOperations.GetBindingExpression(listBox, ListBox.DataContextProperty);
expression.UpdateTarget();