Rac*_*hel 7 wpf xaml styles resourcedictionary
是否可以在样式中定义ResourceDictionary?
例如,假设我想为StackPanels设置两种不同的样式,并且在一种情况下,我希望所有按钮都是蓝色,另一种我希望它们是红色.这可能吗?
就像是
<Style x:Key="RedButtonsPanel" TargetType="{x:Type StackPanel}">
<Setter Property="Orientation" Value="Horizontal" />
<Setter Property="StackPanel.Resources">
<Setter.Value>
<ResourceDictionary>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Red" />
</Style>
</ResourceDictionary>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
上面的代码失败了,关于Setter的Property值的错误不能为null(即使它显然不是null).
我可以做点什么
<ResourceDictionary x:Key="RedButtons">
<Style TargetType="{x:Type Button}">
<Setter Property="Width" Value="100" />
<Setter Property="Background" Value="Red" />
</Style>
</ResourceDictionary>
<StackPanel Resources={StaticResource RedButtons} />
Run Code Online (Sandbox Code Playgroud)
但是我想知道是否有办法将ResourceDictionary合并到样式中.
尝试将Style(s)for every添加TargetType到DockPanel Style.Resources.
我用 a 做了类似的事情DockPanel Style。希望添加到的所有按钮或分隔符DockPanel以一致的方式设置样式。
这是一个示例:
<Style x:Key="DockPanelToolBarStyle" TargetType="{x:Type DockPanel}">
<Style.Resources>
<Style TargetType="Button" BasedOn="{StaticResource ButtonToolBarStyle}" />
<Style TargetType="Separator" BasedOn="{StaticResource SeparatorToolBarStyle}" />
</Style.Resources>
<Setter Property="Height" Value="45"/>
<Setter Property="Background" Value="{StaticResource ToolBarBrush}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)