【发布时间】:2021-05-08 11:54:35
【问题描述】:
我想更改我的两个Buttons 在StackPanel 中被点击时的背景颜色。在Window.Ressources 中,我实现了两个触发器IsMouseOver 和IsPressed。事件IsMouseOver 正在工作,我得到了我想要的悬停效果,但事件IsPressed 不工作。我没有看到问题,有人可以帮助我吗?
<Window x:Class="Einstellungen_awintaONE.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Einstellungen_awintaONE"
mc:Ignorable="d"
Title="awintaONE Einstellungen" Height="500" Width="850">
<Window.Resources>
<Style x:Key="_ButtonStyle" TargetType="Button">
<Setter Property="Foreground" Value="#585757"/>
<Setter Property="Background" Value="#BBBBBB"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="_Border" Padding="2" SnapsToDevicePixels="true">
<ContentPresenter VerticalAlignment="Center" Margin="15,0,0,0" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="_Border" Property="Background" Value="Gray"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="_Border" Property="Background" Value="Gray"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="180" Name="Navigation" />
<ColumnDefinition Name="Einstellungen"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Background="#BBBBBB">
<Grid Grid.Column="0">
<Grid.RowDefinitions >
<RowDefinition Height="40"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1" Grid.Column="0" >
<Button Content="Systeminformationen" Style="{DynamicResource _ButtonStyle}" Height="30" />
<Button Content="Einstellungsassistent" Style="{DynamicResource _ButtonStyle}" Height="30" />
</StackPanel>
</Grid>
</Border>
</Grid>
</Window>
【问题讨论】:
-
尝试设置普通背景:
<Border Name="_Border" Background="{TemplateBinding Background}" Padding="2" SnapsToDevicePixels="true">
标签: wpf xaml button triggers background-color