Change button color when “IsMouseOver” wpf Mahapps
我试图在鼠标悬停在其上时更改mahapp按钮的背景颜色。问题是,如果我使用下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <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> |
在此答案中发现颜色确实发生了变化,但是我的按钮"忘记了"是一个mahapp按钮,即它失去了样式。
如何在不丢失样式的情况下更改颜色?
谢谢
How to change the color without losing the style?
您需要重新定义整个模板:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <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> |
您不能仅覆盖
WPF:是否有一种方法可以重写ControlTemplate的一部分而不重新定义整个样式?
尝试以下操作:
1 2 3 4 5 6 7 8 9 10 11 | <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> |