我试图在按钮中设置多个样式,特别是我:
style(1) => Style="{StaticResource VisibleAnimation}"
style(2) => Style="{DynamicResource AccentedSquareButtonStyle}"
伪代码:
<Button Content="Invia" Style="{DynamicResource AccentedSquareButtonStyle, VisibleAnimation}" ></Button>
编辑:合并可能的解决方案
<Style TargetType="FrameworkElement" x:Key="VisibleAnimation" BasedOn="{DynamicResource AccentedSquareButtonStyle}">
编译器为BasedOn行加下划线并显示我:
无法根据类型样式设置DynamicResourceExtension的属性。您只能为DependencyObject的Dependency属性设置DynamicResourceExtension。
我怎么能做到这一点?
答案 0 :(得分:4)
您可以Style1
basedon
Style2
或反之亦然,然后将顶级style
应用到button
,
<Window.Resources>
<Style TargetType="Button" x:Key="Style1">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
<Style TargetType="Button" x:Key="Style2" BasedOn="{StaticResource Style1}">
<Setter Property="Background" Value="Green"/>
</Style>
但如果您不想改变任何两种样式(例如在其他地方使用它们),请按照此blog post扩展您的按钮样式。
答案 1 :(得分:0)
您只能为控件设置一个Style
。但是你可以&#34;合并&#34;使用Style.BasedOn
属性的样式。例如:
<Style TargetType="Button" x:Key="style1">
<Setter Property="Background" Value="Red"/>
<!-- some other setters here -->
</Style>
<Style TargetType="Button" BasedOn="{StaticResource style1}">
<Setter Property="BorderBrush" Value="Green"/>
<Setter Property="BorderThickness" Value="2"/>
<!-- some othe setters maybe-->
</Style>
第二种风格&#34;合并&#34;他的主持人和制定者来自他的风格。因此,应用第二种样式会将Button
背景设置为红色,将BorderBrush设置为绿色。
第二种样式没有x:Key
属性。这意味着它将自动应用于每个Button
。您只能在一个ResourceDictionary