Tutorial :How to keep datasource updated in all WPF user controls?


I have WPF application with one main window.

In this window there are a series of buttons which form a menu and a space where user controls are loaded at runtime.

However, if the user changes data (stored in XML file) in one user control and then switches to another user control, the user does not see the updated data in the new user control. The user has to restart the application to see the changes.

Here is how my view is bound to the ModelView which in turn gets the data from the model (ObservableCollection):

<UserControl.Resources>      <local:CustomersViewModel x:Key="CustomersDataProvider"/>  </UserControl.Resources>    <ListBox       ItemsSource="{Binding Path=GetAll, Source={StaticResource CustomersDataProvider}}"       ItemTemplate="{StaticResource CustomersDataTemplate}"/>  

So how can I now, with the databinding being defined solely in the XAML, tell the view to refetch the data from the viewmodel (perhaps in the constuctor of the code behind class)?


The problem is probably due to the resource being declared in your UserControl's resources. This means that every UserControl will have its own copy of the resource and they will not be in sync. Consider using a shared resource.


Have you tried setting the Mode=TwoWay on the Binding?

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »