我有一个用户控件库,其中包含一些资源字典。代码:
<ResourceDictionary ... >
<LinearGradientBrush x:Key="MyButtonBackground" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF654073" Offset="0.084"/>
<GradientStop Color="#FF8A6093" Offset="0.929"/>
</LinearGradientBrush>
<Style x:Key="MyButtonStyle" TargetType="{x:Type MyButton}" >
<Setter Property="Background" Value="{StaticResource ResourceKey=MyButtonBackground}" />
<Setter Property="Foreground" Value="White" />
</Style>
</ResourceDictionary>
然后我有一个类来加载资源字典。基本上:
return (ResourceDictionary)Application.LoadComponent(new System.Uri("/MyAssembly;component/Themes/Default.xaml", System.UriKind.Relative))
现在,在 UserControl 类中,获取 ResourceDictionary 后,我想直接加载 Style。我怎样才能做到这一点?
this.Style = ((Style)MyResourceDictionary["MyButtonStyle"]); // Don't work
然而:
this.Background = ((Brush)MyResourceDictionary["MyButtonBackground"]); // Works