I am trying to instantiate a control in my window's code behind (for Frame Navigation). Unfortunately, it's not getting the default style applied to it.
I would expect that when a control is first rendered, the renderer would set the control's Style/Template to either (the explicitly supplied one -> the default one supplied in the resources -> null).
Is my mental model of how default styles are applied wrong? Is it actually like how
class Whatever
{
int value = 5;
}
is really syntactic sugar for
class Whatever
{
public Whatever()
{
this.value = 5;
}
int value;
}
And thus the Style is being set at compile time?
Could it be a problem stemming from how I'm accessing the Styles and Templates for the control (unlikely as I can make a control of it's type on my main window and it has the default Style)?
MainWindow.xaml
<Window>
<Window.Resources>
<ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="DataLogPage/Themes.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<!-- Data here... -->
</Window>
DatalogPage/Themes.xaml
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/Styles/DefaultStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
DatalogPage/Themes/Styles/DefaultStyle.xaml
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Templates/DefaultTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="this:DataLog">
<Setter Property="Template"
Value="{StaticResource DefaultTemplateForDataLog}" />
</Style>
</ResourceDictionary>
DatalogPage/Themes/Templates/DefaultTemplate.xaml
<ResourceDictionary>
<ControlTemplate x:Key="DefaultTemplateForDataLog"
TargetType="this:DataLog">
<!-- Data here... -->
</ControlTemplate>
</ResourceDictionary>
MainWindow.xaml.cs (where I'm creating the control in the code behind)
private void currentContext_ContextChangeRequested()
{
//Other bits of logic for context switching
//User wants to go to the Data Log Page
this.MainFrame.Navigate(new DataLogPage.DataLog());
return;
}
To reiterate:
Problem: Control created in code behind does not have it's default Style.
Possible ideas on why this may be:
1)My user model for how default styles are applied is wrong and I must set it explicitly.
2)I may be referencing the style incorrectly.
If I have to explicitly set the Style/Template, is there a best-of-both-worlds where I can make something in the MainWindow's xaml that I can programmatically reference like so: new DataLogPage.DataLog(){Style = this.DataLogStyle};
?