UserControl中的IsMouseOver

时间:2014-08-29 22:59:34

标签: c# wpf wpf-controls

我试图捕捉 - 修改IsMouseOver以更改MouseEnter上的Button的BackColor和MouseLeave:

 <UserControl x:Class="TVSlide.TitleBarButton"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 mc:Ignorable="d"
                 d:DesignHeight="400" d:DesignWidth="400">

        <Grid>
            <Button Name="TButton">
                <Button.Style>
                    <Style>
                        <Setter Property="Button.Background" Value="Blue"/>
                        <Style.Triggers>
                            <Trigger Property="Button.IsMouseOver" Value="True">
                                <Setter Property="Button.Background" Value="Yellow" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>

            </Button>

            <!--<Button Name ="TButton" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top"
                    Width="121" Height="44" Background="#FFEC0D0D" />-->
        </Grid>
    </UserControl>

仍然变得可怕的冰蓝而不是黄色。

1 个答案:

答案 0 :(得分:0)

您需要覆盖按钮模板并删除默认的chrome

这是一个示例

<Button Name="TButton"
        Content="A button">
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="Background"
                    Value="Blue" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid x:Name="border"
                              Background="{TemplateBinding Background}"
                              SnapsToDevicePixels="true">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              Margin="{TemplateBinding Padding}"
                                              RecognizesAccessKey="True"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled"
                                     Value="false">
                                <Setter Property="Foreground"
                                        Value="{StaticResource {x:Static SystemColors.InactiveCaptionTextBrushKey}}" />
                                <Setter Property="Opacity"
                                        TargetName="border"
                                        Value="0.5" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver"
                         Value="True">
                    <Setter Property="Background"
                            Value="Yellow" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>