4

I have a Window in WPF which simply contains a Frame element. The Frame displays a Page; which Page is displayed changes based on user interaction.

<Window x:Class="MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="720" Width="1280">
    <Grid>
        <Frame Source="{Binding Source={StaticResource MainPageIntent}, Path=Path}"/>
    </Grid>
</Window>

I would like all Pages that appear in that Frame to share a common Resource Dictionary so that they may all be styled in a common way.

Right now I have something like this in every page that this Window loads:

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/ResourceDictionaries/BaseControlStyles/MenuStyle.xaml"/>

I was hoping that I might just be able to set the resource dictionary on the Window, and they would "inherit" those resources, but that does not appear to be the case. I tried something like this, but the styles found in MenuStyle.xaml are not applied the the controls inside the Page loaded by the Frame:

<Window x:Class="MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="720" Width="1280">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/ResourceDictionaries/BaseControlStyles/MenuStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Frame Source="{Binding Source={StaticResource MainPageIntent}, Path=Path}"/>
    </Grid>
</Window>

Is there are way to define styles at the Window level such that all pages loaded in child Frames will use those styles?

Note: I do not want to apply these styles to ALL windows in my application, so putting this ResourceDictionary in my App.xaml does not appear to be a valid solution.

Goose
  • 1,307
  • 2
  • 14
  • 28
  • Window is not logical parent of Page. Hence it could not resolve the resource from its dictionary. So, last resort would be to merge it in App resources Or merge it in every instance of page in constructor of Page. – Rohit Vats Oct 03 '14 at 16:24

1 Answers1

1

If you want to write it once to avoid code duplicates, you can write it in code behind. On frame ContentRendered you can write a code to add resource to the page which is being loaded.

<Frame Name="fr_View" ContentRendered="fr_View_ContentRendered"/>


private void fr_View_ContentRendered(object sender, System.EventArgs e)
{
     ResourceDictionary myResourceDictionary = new ResourceDictionary();
     myResourceDictionary.Source = new Uri("Dictionary1.xaml", UriKind.Relative);
     (fr_View.Content as System.Windows.Controls.Page).Resources.MergedDictionaries.Add(myResourceDictionary);
}

Take a look at this link: Set up application resources from code

Community
  • 1
  • 1