次のように、リソース ディクショナリで 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 からスタイルを抽出する方法はありますか?