【问题标题】:Change button color when "IsMouseOver" wpf Mahapps“IsMouseOver” wpf Mahapps 时更改按钮颜色
【发布时间】:2017-10-18 09:09:10
【问题描述】:


当鼠标移过它时,我正在尝试更改 mahapp 按钮的背景颜色。问题是,如果我使用下面的代码:

<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="50" Height="50" HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Margin="50,0,0,0">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Background" Value="Green"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type 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="DarkGoldenrod"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

发现 in this answer 颜色确实发生了变化,但我的按钮“忘记”成为 mahapp 按钮,即它失去了样式。
如何在不失风格的情况下改变颜色?
谢谢

【问题讨论】:

  • 尝试提供基本样式:&lt;Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}"&gt;
  • 当我修改其他属性如 Font family 声明

标签: wpf xaml mahapps.metro


【解决方案1】:

如何在不丢失样式的情况下改变颜色?

你需要重新定义整个模板:

<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="50" Height="50" 
                HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Margin="50,0,0,0">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="{DynamicResource FlatButtonBackgroundBrush}" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="FontSize" Value="{DynamicResource FlatButtonFontSize}" />
            <Setter Property="Foreground" Value="{DynamicResource FlatButtonForegroundBrush}" />
            <Setter Property="Padding" Value="10 5 10 5" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}" xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro">
                        <Border x:Name="Border"
                            Margin="0"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                            <Controls:ContentControlEx x:Name="PART_ContentPresenter"
                                                   Padding="{TemplateBinding Padding}"
                                                   HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                   VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                                   Content="{TemplateBinding Content}"
                                                   ContentCharacterCasing="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
                                                   ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                                   ContentTemplate="{TemplateBinding ContentTemplate}"
                                                   ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
                                                   RecognizesAccessKey="True"
                                                   SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="DarkGoldenrod" />
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="{DynamicResource FlatButtonPressedBackgroundBrush}" />
                    <Setter Property="Foreground" Value="{DynamicResource FlatButtonPressedForegroundBrush}" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="{DynamicResource GrayBrush2}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

您不能只覆盖ControlTemplate 的一部分:

WPF: Is there a way to override part of a ControlTemplate without redefining the whole style?

【讨论】:

    【解决方案2】:

    尝试以下方法:

    <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="50" Height="50" HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Margin="50,0,0,0">
        <Button.Style>
            <Style BasedOn="{StaticResource {x:Type Button}}" TargetType="Button">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Aqua"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>
    

    【讨论】:

    • 这样样式不变但颜色不变,我就是这样用模板的
    • 这是问题的正确答案,比重新定义模板简单多了。不知道为什么人们不赞成
    • 这实际上对我不起作用。 @mm8 的解决方案虽然有效。
    猜你喜欢
    • 2016-10-13
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    相关资源
    最近更新 更多