为什么这个XAML不能按预期工作?

时间:2017-12-18 01:00:18

标签: wpf xaml triggers

当我的鼠标移过它时,我正试图改变按钮的默认行为。以下是代码的一部分: (我省略了各种面板的代码,只保留了感兴趣的面板的代码)

<Window.Resources>
    <Style x:Key="leftPanelButtons" TargetType="{x:Type Button}">
        .... various setters go here ....
        <Setter Property="Background" Value="ForestGreen"/>

        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="DarkSeaGreen"/>
            </Trigger>
        </Style.Triggers>

    </Style>

</Window.Resources>

<DockPanel Grid.Row="2">
        <DockPanel Height="Auto" Width="Auto" VerticalAlignment="Stretch">
            <UniformGrid Columns="1" Height="Auto" VerticalAlignment="Stretch" MinWidth="150" Width="Auto">
                <UniformGrid.Resources>
                    <Style BasedOn="{StaticResource leftPanelButtons}" TargetType="Button"/>
                </UniformGrid.Resources>

                <Button>
                    button1
                </Button>

                <Button>
                    button2
                </Button>

                <Button>
                    button3
                </Button>

            </UniformGrid>

            <StackPanel Height="Auto" Width="Auto" Background="PaleGoldenRod">

            </StackPanel>
        </DockPanel>
    </DockPanel>

我希望将按钮背景颜色设置为ForestGreen,并在鼠标移过按钮时更改为DarkSeaGreen。我确实得到了ForestGreen颜色,但是当我将鼠标移到按钮上时,颜色变为浅蓝色变体。我有点复制了这个page的代码。

1 个答案:

答案 0 :(得分:0)

是的,有点奇怪。要修改MouseOver上按钮的背景,您实际上必须更改ControlTemplate。见这里:

How do you change Background for a Button MouseOver in WPF?