0

I am trying to get this example working. I created a new WPF Custom Controls Library project and in Generic.xaml I have the following code

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfCustomControlLibrary1">

<ControlTemplate x:Key="myControlTemplate1">
    <TextBlock Text="This text should appear"></TextBlock>
</ControlTemplate>
<Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">

                    <ContentControl Template="{DynamicResource myControlTemplate1}"></ContentControl>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

When I use my control in an application, I do not see the TextBlock. Why ?

But if I change it to use the template as StaticResource, it works. Why ?

fahadash
  • 3,133
  • 1
  • 30
  • 59
  • possible duplicate of [WPF StaticResource works, DynamicResource doesn't](http://stackoverflow.com/questions/3537107/wpf-staticresource-works-dynamicresource-doesnt) – fahadash Oct 29 '14 at 05:44

1 Answers1

0

I think the answer is in here (last sentence in the text below):

Static resource lookup can extend into themes, or into system resources, but this is supported only because the XAML loader defers the request. The deferral is necessary so that the runtime theme at the time the page loads applies properly to the application. However, static resource references to keys that are known to only exist in themes or as system resources are not recommended. This is because such references are not reevaluated if the theme is changed by the user in realtime. A dynamic resource reference is more reliable when you request theme or system resources. The exception is when a theme element itself requests another resource. These references should be static resource references, for the reasons mentioned earlier.

You could merge Generic.xaml in your app resources though, then the resource lookup doesn't extend into the theming.

Rob van Daal
  • 298
  • 1
  • 9