MouseOver样式触发器不会更改背景

时间:2017-06-01 02:55:40

标签: wpf xaml wpf-style

WPF新手,来自网络背景。

我的样式触发器不会更改按钮背景。风格XAML:

<Window.Resources>
    <Style TargetType="Button">
        <Setter Property="Background" Value="GhostWhite" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="#F48230"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources> 

按钮XAML(与窗口或网格属性无关):

<Window...>
    <Grid...>
        <StackPanel Orientation="Horizontal" Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center">
            <Button Name="btnEdit" Cursor="Hand" Content="Edit Settings..." HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Margin="0,0,5,0" Click="btnEdit_Click"/>
            <Button Name="btnExit" Cursor="Hand" Content="Exit" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Margin="5,0,0,0" Click="btnExit_Click" />
        </StackPanel>
    </Grid>
</Window>

按钮确实会在资源部分中选择背景样式,但不会触发 - 鼠标悬停会导致默认行为。

补充问题:有没有办法可以调试这个?我查看了Live Visual Tree,但无法弄清楚如何获取我需要的信息。

1 个答案:

答案 0 :(得分:1)

WPF控件具有 ControlTemplate 类型的 Template 属性。此属性告诉WPF如何在屏幕上绘制控件。 WPF按钮在其中使用Windows Chrome ControlTemplate ,它使用用户选择的系统颜色来实现不同应用程序之间的一致性。利用WPF和XAML的神奇功能,您可以创建自己的 ControlTemplate ,使按钮看起来更合适。

使用键创建样式,以便您可以选择使用模板的按钮:

<Window.Resources>
    <Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="GhostWhite" />
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <!-- a simple square button -->
                    <Border Name="wrapper"
                            Width="{TemplateBinding Width}"
                            Height="{TemplateBinding Height}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Margin="{TemplateBinding Margin}"
                            Background="#01000000">
                        <!-- notice the wrapper has a background that is NEAR transparent. This is important. It'll ensure the button raises the click event -->
                        <Border Name=inner
                                Background="{TemplateBinding Background}">
                            <ContentPresenter Margin="{TemplateBinding Padding}"
                                              HorizontalAlignment="Center"
                                              VerticalAlignment="Center" />
                        </Border>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
       <Style.Triggers>
           <Trigger Property="IsMouseOver" Value="True">
               <Setter Property="Background" Value="#FFF48230"/>
           </Trigger>
           <Trigger Property="IsPressed" Value="true">
               <Setter Property="Background" Value="#FF99501B"/>
           </Trigger>
       </Style.Triggers>
    </Style>
</Window.Resources> 

然后在按钮上使用模板:

<Window...>
    <Grid...>
        <StackPanel Orientation="Horizontal" Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center">
            <Button Style="{StaticResource MyButtonStyle}" Name="btnEdit" Cursor="Hand" Content="Edit Settings..." HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Margin="0,0,5,0" Click="btnEdit_Click"/>
            <Button Style="{StaticResource MyButtonStyle}" Name="btnExit" Cursor="Hand" Content="Exit" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Margin="5,0,0,0" Click="btnExit_Click" />
        </StackPanel>
    </Grid>
</Window>
相关问题