Sharing ObjectDataProvider in WPF

I am trying to share an ObjectDataProvider resource between my main application and user control. I define odp in a separate resource dictionary file that is included in the application and user control.

<ObjectDataProvider x:Key="AsymmetricFrameHolder" ObjectType="{x:Type data:DataFrameAsymmetric}"/> 

Then I will try to access this in the main application as follows:

 ObjectDataProvider odp = (ObjectDataProvider)Resources["AsymmetricFrameHolder"]; return (DataFrameAsymmetric)odp.ObjectInstance; 

And bind it in the user control with:

 <Grid Name="grid" Height="Auto" Width="Auto" DataContext="{StaticResource AsymmetricFrameHolder}"> 

Then:

 <TextBox Name="TextBox_Length" Grid.Row="0" Grid.Column="1" Text="{Binding Path=Length }"/> 

This creates 2 instances of DataFrameAsymmetric. One in the main application and one in the user control.

How to configure the program so that one common instance is created?

+4
source share
1 answer

If you can go the other way, this problem can be solved easily. Convert your DataFrameAsymmetric class to a Singleton class, and wherever you want to use the instance, use it below

 <Grid Name="grid" Height="Auto" Width="Auto" DataContext="{Binding Source={x:Static data:DataFrameAsymmetric.Instance}}"> 

In any part of your code, you can access the instance using

 DataFrameAsymmetric.Instance 
0
source

All Articles