我在我的资源字典中定义了一个ComponentResourceKey,如下所示:
<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:Resources}, ResourceId=BaseControlStyle}" TargetType="{x:Type FrameworkElement}">
<Setter Property="Margin" Value="4,4,0,0" />
</Style>
我有一个静态类,我用它作为提供资源键的快捷方式,就像这样:
public class Resources
{
public static ComponentResourceKey BaseControlStyleKey
{
get
{
return new ComponentResourceKey(typeof(Resources), "BaseControlStyle");
}
}
}
通常情况下,当我使用这种风格时,我会做类似于这样的事情:
<TextBlock Style="{DynamicResource {x:Static local:Resources.BaseControlStyleKey}}"/>
然而,我有一个场景需要在代码中设置样式,例如:
myTextBox.Style = Resources.BaseControlStyleKey // Does not work.
有什么想法可以从ComponentResourceKey中提取样式吗?