eyeballpaul
eyeballpaul

Reputation: 1735

WPF - how to reference a style in a resource when the resource dictionary has a key set

I have created a user control that needs to reference an external resource dictionary file. A style within this resource file is then used against a textbox in the user control.

The external resource dictionary file is as follows:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style x:Key="ValidatedTextboxStyle" TargetType="{x:Type TextBox}">
    ...
</Style>

I then import this into the user control as follows: (with the long file location removed)

<Control.Resources>
    <ResourceDictionary x:Key="Test"  Source="..." />
    <common:StringCollapseVisibilityConverter x:Key="StringCollapseVisibilityConverter" />
</Control.Resources>

The WPF designer forces me to give it a "key" due to the other resource that is referenced.

Without the dictionary having a name, you would normally reference it like:

Style="{StaticResource ValidatedTextboxStyle}"

How would I reference the style that has name "ValidatedTextboxStyle" within the external resource file taking into account that the imported resource dictionary is given the key name "Test"?

Upvotes: 1

Views: 2866

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81313

Merged the dictionary in your control and you can use it like earlier via StaticResource.

<Control.Resources>
   <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary Source="..." />
      </ResourceDictionary.MergedDictionaries>
      <common:StringCollapseVisibilityConverter
                              x:Key="StringCollapseVisibilityConverter" />
   </ResourceDictionary>
</Control.Resources>

Also, you can omit setting x:Key now on resource dictionary since all the defined resources in resource dictionary are merged into your control resources.

Now, you can use like earlier:

Style="{StaticResource ValidatedTextboxStyle}"

Upvotes: 1

Related Questions