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

佐格

当鼠标移到按钮上时,我在尝试更改按钮的默认行为时感到头疼。这是代码的一部分:(我省略了各种面板的代码,只保留了感兴趣的面板的代码)

<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 颜色,但是当我将鼠标移到按钮上时,颜色会变为浅蓝色变体。我已经从这个页面复制了代码

ATH

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

如何在 WPF 中更改 Button MouseOver 的背景?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章