WPF - Refresh DataTemplate Content

I have a tab that has its contents set to an object (TFS WorkItem). I have a DataTemplate for type WorkItem.

When I set an object to a tab, it displays well.

However, when I update one of the collections of an object (list of links), this change is not updated for presentation.

I tried to make my WorkItem DependencyProperty, and I also tried to set the contents of the tab content to null again, and then to my object (in the hope that it would reload it).

None of this works.

Normally I would just use an observable collection to store links, but since I don't have the WorkItem class, I need another solution that will manually update the DataTemplate.

Any ideas?

+4
source share
1 answer

To force bind to update the user interface, call BindingExpression.UpdateTarget. To get the binding expression for a given element (in your case, I assume that ItemSource), use BindingOperations.GetBindingExpression. For instance.

BindingExpression bindingExpr = BindingOperations.GetBindingExpression(linksListBox, ListBox.ItemsSourceProperty); bindingExpr.UpdateTarget(); // refreshes the ItemsSource 

However, this depends on having a reference to a control whose property is related, which can be difficult if the control is in a DataTemplate. You could try running UpdateTarget () depending on which control the DataTemplate is placing (tab?) And which property is bound to the WorkItem (Content property?), But I have not tested this. (I would be interested to know if this works!)

+4
source

All Articles