1

I have a WPF Window:

<Window x:Class="MyUI.MainWindow"
        and so on>
    <Window.Resources>
        <ResourceDictionary>
            <Style TargetType="{x:Type s:SurfaceListBox}" x:Key="FatherStyle" >
        </ResourceDictionary>
    </Window.Resources>
</Window>

I have a resource dictionary in MyResourceDictionary.xaml:

<ResourceDictionary xmlns="........."
                    and so on >
    <Style TargetType="{x:Type s:SurfaceListBox}" x:Key="ChildStyle"  BasedOn="{StaticResource FatherStyle}" />
</ResourceDictionary>

But when I try to reference ChildStyle from MyUI.Window:

<Window as shown in 1st block of code above>
    <s:SurfaceListBox Style="{StaticResource ResourceKey=ChildStyle}" />
</Window>

It tells me that it can't find FatherStyle. I read here and merged the dictionary in MyResourceDictionary.xaml:

<ResourceDictionary xmlns="........."
                    and so on >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="MainWindow.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style ChildStyle as shown above />
</ResourceDictionary>

Now it tells me it can't find ChildStyle. How do I reference this properly?

Community
  • 1
  • 1
Darren Ng
  • 373
  • 1
  • 5
  • 20

1 Answers1

1

You can't reference a resource dictionary contained in a Window-type XAML file, from another file. What you need to do is create a separate resource dictionary, "Shared.xaml" or whatever:

<ResourceDictionary ... >
    <Style TargetType="{x:Type s:SurfaceListBox}" x:Key="FatherStyle" >
</ResourceDictionary>

Now reference the shared one from your main window:

... and also from "MyResourceDictionary.xaml":

<ResourceDictionary xmlns="........."
                and so on >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Shared.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style TargetType="{x:Type s:SurfaceListBox}" x:Key="ChildStyle"  BasedOn="{StaticResource FatherStyle}" />
</ResourceDictionary>

And now in your "MyUI.xaml" window, you should be able to access the "ChildStyle" as you expected, by referencing "MyResourceDictionary":

<ResourceDictionary xmlns="........."
                and so on >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="MyResourceDictionary.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <s:SurfaceListBox Style="{StaticResource ResourceKey=ChildStyle}" />
</ResourceDictionary>
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • 1
    Thanks! But the reason why I put fatherstyle in the window-type xaml file is because it contains the event handlers. I will have a much easier time when the event handler is together with the window xaml as they will need to share data with each other. But since it can't be done, I'll just follow your method and think of another way. Thanks! – Darren Ng Feb 17 '14 at 06:14
  • 1
    Ah, I found this to solve my issue: http://stackoverflow.com/questions/5745001/xaml-combine-styles – Darren Ng Feb 17 '14 at 06:46