I am trying to share an ObjectDataProvider resource between my main application and a user control. I define the odp in a separate resource dictionary file that is included in the app and the user control.
<ObjectDataProvider x:Key="AsymmetricFrameHolder" ObjectType="{x:Type data:DataFrameAsymmetric}"/>
I then try to access this in the main app as follows
ObjectDataProvider odp = (ObjectDataProvider)Resources["AsymmetricFrameHolder"];
return (DataFrameAsymmetric)odp.ObjectInstance;
and bind to 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 app and one in the user control. How can I set the program so that a single shared instance is created?
From stackoverflow
-
If you can take a different route this problem can be solved easily. Convert your Class DataFrameAsymmetric in to Singleton class and at any place you want to use the instance of it use as bellow
<Grid Name="grid" Height="Auto" Width="Auto" DataContext="{Binding Source={x:Static data:DataFrameAsymmetric.Instance}}">
At any part of your code you will be able to access the instance by
DataFrameAsymmetric.Instance
Dave Turvey : Thanks for the answer but a singleton wouldn't work here. I will need to make more than one instance of the class in the program.
0 comments:
Post a Comment