在Style类中是否可以具有<style.triggers>属性?

时间:2019-04-11 07:32:30

标签: c# wpf xaml triggers styling

当鼠标悬停在顶部时,我希望有一个触发器来将按钮颜色设置为紫色,我希望其代码包含在x:Key样式中,以便可以简单地重复使用。

<Style x:Key="MinMaxClose" TargetType="Button">
            <Setter Property="Height" Value="24"/>
            <Setter Property="Width" Value="24"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="FontFamily" Value="Segoe MDL2 Assets"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Purple"/>
                </Trigger>
            </Style.Triggers>
        </Style>

以上操作无效,我目前被迫使用:

<Button.Style>
               <Style TargetType="Button">
                   <Setter Property="Background" Value="DarkSlateBlue"/>
                   <Setter Property="Template">
                       <Setter.Value>
                           <ControlTemplate TargetType="Button">
                               <Border Background="{TemplateBinding Background}">
                                   <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                               </Border>
                           </ControlTemplate>
                       </Setter.Value>
                   </Setter>
                   <Style.Triggers>
                       <Trigger Property="IsMouseOver" Value="True">
                           <Setter Property="Background" Value="Purple"/>
                       </Trigger>
                   </Style.Triggers>
               </Style>
           </Button.Style>
       </Button>
https://stackoverflow.com/a/20073418找到的

0 个答案:

没有答案