Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Refresh StaticResource

I have a ComboBox that has it's ItemsSource bound as

ItemsSource="{Binding Source={StaticResource documentTemplates}}"

Where documentTemplates is

<ObjectDataProvider x:Key="documentTemplates"
                    ObjectType="{x:Type Core:DataHelper}"
                    MethodName="GetDocumentTemplates"/>

The problem I have is that the document templates defined in the database could be changed by other areas of the application (or indeed by another user) and so I want to have the ItemsSource requery each time. At the moment, once the resource has been populated it will never requery. I assume that this is because it is a StaticResource, but if I swap this for a DynamicResource I get

A 'DynamicResourceExtension' cannot be set on the 'Source' property of type 'Binding'. A 'DynamicResrouceExtension' can only be set on a DependencyProperty or a DependencyObject

How should I go about fixing this?

like image 362
David Ward Avatar asked Dec 22 '22 20:12

David Ward


1 Answers

Keep your XAML as is and whenever a requery is needed call Refresh on the ObjectDataProvider.

(FindResource("documentTemplates") as ObjectDataProvider).Refresh();
like image 119
Wallstreet Programmer Avatar answered Jan 02 '23 20:01

Wallstreet Programmer