WPF 按钮属性触发器的问题

当前按钮按下时自动会变成灰色,想改成当按钮按下变成Green。Application.Resources中加了<StyleTargetType="Button"x:Key... 当前按钮按下时自动会变成灰色,想改成当按钮按下变成Green。
Application.Resources中加了
<Style TargetType="Button" x:Key="ButtonStyle">
<Style.Triggers>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" Value="Green" />
<Setter Property="FontSize" Value="39" />
</Trigger>
</Style.Triggers>
</Style>
结果发现button中字体大小变了(说明触发器有效),但按钮颜色仍然是变成灰色。不知道是哪设置错了....高手速来啊...
展开
 我来答
cnmahj
推荐于2016-11-30 · TA获得超过954个赞
知道小有建树答主
回答量:785
采纳率:0%
帮助的人:1066万
展开全部
原因是你在IsPressed时候设置的是Button的Background,这个值是用来确定Button正常显示时的背景的。因此没有反映。你可以通过重新定义Button的Template来解决该问题。给你个例子。你看看。

保存成文件后直接在浏览器中就能看到效果。IE、FF都可以

------------

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">

<Page.Resources>

<Style x:Key="InformButton" TargetType="Button">
<Setter Property="Margin" Value="2"/>
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="11px"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0" />
<GradientStop Color="Black" Offset="0.2" />
<GradientStop Color="#007F7F7F" Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border"
BorderThickness="2"
Padding="4,2"
BorderBrush="DarkGray"
CornerRadius="13"
Width="50"
Height="50"
Background="{TemplateBinding Background}">
<Grid >
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="contentShadow">
<ContentPresenter.RenderTransform>
<TranslateTransform X="1.0" Y="1.0" />
</ContentPresenter.RenderTransform>
</ContentPresenter>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="#FF4788c8" />
<Setter Property="Foreground" Value="#FF4788c8" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0" />
<GradientStop Color="Blue" Offset="0.2" />
<GradientStop Color="#007F7F7F" Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter TargetName="content" Property="RenderTransform" >
<Setter.Value>
<TranslateTransform Y="1.0" X="1.0" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsDefaulted" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="#FF282828" />
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="#FF282828" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Grid Height="306" Width="242">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />

</Grid.ColumnDefinitions>
<Border BorderBrush="Silver" BorderThickness="1" Grid.ColumnSpan="5" Grid.RowSpan="4" Height="240" HorizontalAlignment="Left" VerticalAlignment="Top" Width="240" />
<Button Style="{StaticResource InformButton}" Width="50" Height="50" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5,5,3,3">
<Path Fill="Green" Data="M 3,3 l 9,9 l -9,9 Z" />
</Button>
<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Column="1">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Column="3">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Column="4">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="1">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button Grid.Column="1" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="1">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button Grid.Column="3" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="1">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button Grid.Column="4" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="1">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="2">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button Grid.Column="1" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="2">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button Grid.Column="3" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="2">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button Grid.Column="4" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="2">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="3">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button Grid.Column="1" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="3">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button Grid.Column="3" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="3">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button Grid.Column="4" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="3">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Border BorderBrush="Silver" BorderThickness="1" Grid.ColumnSpan="5" Height="63" HorizontalAlignment="Left" VerticalAlignment="Top" Width="240" Grid.Row="4">
<Rectangle Stroke="Transparent" Fill="LightGoldenrodYellow" />
</Border>
<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="4">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button Grid.Column="1" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="4">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button Grid.Column="3" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="4">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
<Button Grid.Column="4" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="4">
<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />
</Button>
</Grid>

</Page>
nameyanzaode
2010-08-19 · TA获得超过147个赞
知道小有建树答主
回答量:116
采纳率:0%
帮助的人:93.1万
展开全部
button 按下取的是系统默认的颜色
你想更改 需要把button的系统样式重写
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式