Wpf – How to get the actual resource from a ComponentResourceKey


I have a ComponentResourceKey defined in my resource dictionary like this:

<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:Resources}, ResourceId=BaseControlStyle}" TargetType="{x:Type FrameworkElement}">
    <Setter Property="Margin" Value="4,4,0,0" />

I have a static class that I use as a shortcut to provide the resource keys liek this:

public class Resources
    public static ComponentResourceKey BaseControlStyleKey
            return new ComponentResourceKey(typeof(Resources), "BaseControlStyle");

Now typically when I use this style I do something like this:

<TextBlock Style="{DynamicResource {x:Static local:Resources.BaseControlStyleKey}}"/>

However, I have a scenario where I need to set a style in code like this:

myTextBox.Style = Resources.BaseControlStyleKey // Does not work.

Any ideas how I extract the style from the ComponentResourceKey?

Best Solution

I figured it out.

myTextBox.Style = 
        as Style;