I have a class for my data, MyData
, and I'm accessing it in my gui by data binding. I decided to set the DataContext of the main panel in my window to an instance of this object so I could bind my various controls to it's members. So I could easily reference it in my xaml, I created an instance of MyData
in Window.Resources
like so:
<local:MyData x:Key="myDataInstance"/>
Then I get a reference of this for my code because I need it there sometimes too.
MyData myDataInstance;
public MainWindow()
{
InitializeComponent();
myDataInstance = (MyData)FindResource("myDataInstance");
}
This works well, but I also can load another instance of MyData
by deserializing it from file.
myDataInstance = (myData)serializer.Deserialize(fileStream);
I thought I could simply reassign myDataInstance
in the code like this, but that doesn't seem to be working since my gui doesn't change to reflect the new data. I think reassigning is breaking the link between the main panel's DataContext and myDataInstance
.
- Is it possible to reassign an object declared in xaml as I have?
- From xaml, is it possible to access
members declared only in the code
(the opposite of
FindResource()
)? - If so, how?
Thanks. (btw, Of course I can use other methods to easily solve this problem, but I'm also just interested in the answer to this question.)