I have a wpf navigation application. I have an object that i want to access from different pages. Currently, the object is only available from the page that creates it.
Here what I did.
In page A.xaml
I defined in the page like so
<UserControl.Resources>
<local:SerialComm x:Key="SerialCommDataSource" d:IsDataSource="True" />
</UserControl.Resources>
Above method works except the scope is only limited to the page. I want to be able to access the object from any other page.
I use the object in the page like so
<Grid Style="{StaticResource ContentRoot}" DataContext="{Binding Source={StaticResource SerialCommDataSource}}" >
<TextBox x:Name="tbbaudRate" Height="23" TextWrapping="Wrap" MinWidth="200" Text="{Binding SerialPort.BaudRate}" />
</Grid>
I want to put the resource in App.xaml so that I can access it anywhere.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Blah;component/Assets/Blah.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<local:SerialComm x:Key="SerialCommDataSource" d:IsDataSource="True" /> <--Error here
</Application.Resources>
How do i create a global object in xaml? The object must be able to be edited anywhere and edited values are updated. I read that one cannot create object in code behind and access it anywhere in xaml. So how do i do this in xaml?