I use a ResourceProvider
as global management for my ResourceDictionaries. I register new ResourceDictionaries to the ResourceProvider and raise a event for each affected FrameworkElement. The FrameworkElement then updates its resources with the following method: (I tried several ways to fix this and at the End i tried to change the DataTemplate with its Uri)
public void UpdateResources(FrameworkElement elementToUpdate)
{
foreach(var controlDict in _registeredResources.Where(a => a.ControlType == elementToUpdate.GetType()))
{
//elementToUpdate.Resources.MergedDictionaries.Clear();
//elementToUpdate.Resources.MergedDictionaries.Add(controlDict);
//elementToUpdate.Resources = controlDict;
ResourceDictionary dict =new ResourceDictionary() { Source = new Uri("pack://application:,,,/ApplicationCore.UITest;component/NewDataTemplate.xaml") };
elementToUpdate.Resources = dict;
//elementToUpdate.Resources.MergedDictionaries.Clear();
//elementToUpdate.Resources.MergedDictionaries.Add(controlDict);
}
}
Right now when i press my Button to Change the DataTemplate the ui doesn't refresh with the new template. I have to mention that I'm not changing the object itself.
<ctrl:TreeViewControl DataContext="{Binding}">
<ctrl:TreeViewControl.Resources>
<ResourceDictionary Source="pack://application:,,,/OldDataTemplate.xaml"/>
</ctrl:TreeViewControl.Resources>
</ctrl:TreeViewControl>
My Question: Is it possible to change a DataTemplate during Runtime and refresh the UI without changing the bound object itself?
EDIT: I continued testing: The ResourceDictionary (with its Template) is changed. A new added item (after the Template change) uses the new Template. But the old items are not updated.
DataContext
tonull
and then restore it (maybe with dispatcher invoke). – Sinatr