WPF 按钮属性触发器的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 按钮属性触发器的问题相关的知识,希望对你有一定的参考价值。
当前按钮按下时自动会变成灰色,想改成当按钮按下变成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中字体大小变了(说明触发器有效),但按钮颜色仍然是变成灰色。不知道是哪设置错了....高手速来啊...
保存成文件后直接在浏览器中就能看到效果。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> 参考技术A button 按下取的是系统默认的颜色
你想更改 需要把button的系统样式重写
以上是关于WPF 按钮属性触发器的问题的主要内容,如果未能解决你的问题,请参考以下文章
WPF:一旦我在代码中设置了一个属性,它就会永远忽略 XAML 绑定......我该如何防止这种情况发生?