本文介绍了WPF风格:如何改变渐变停止颜色触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个按钮样式:
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Path x:Name="path1" ... Data="...some data...">
<Path.Fill>
<LinearGradientBrush EndPoint="0.5,-0.3" StartPoint="0.5,0.8">
<GradientStop x:Name="gs1" Color="Green" Offset="0.44"/>
<GradientStop Color="Black" Offset="0.727"/>
</LinearGradientBrush>
</Path.Fill>
</Path>
<ContentPresenter ...properties... />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="???" Property="Color" Value="Green"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
我想改变渐变停止的颜色为x:名称=GS1,当鼠标移动到按钮,所以我用触发器IsMouseOver。我怎样才能在触发到颜色属性的访问?我试过的TargetName =GS1和的TargetName =path1.gs1,但它不工作。任何想法?
I want to change the Color of GradientStop with x:Name="gs1" when mouse is over button, so I use Trigger IsMouseOver. How can i get an access to Color Property in Trigger? I tried TargetName="gs1" and TargetName="path1.gs1" but it doesn't work. Any idea?
推荐答案
试试这个:
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
<Style.Resources>
<LinearGradientBrush x:Key="gs1" EndPoint="0.5,-0.3" StartPoint="0.5,0.8">
<GradientStop Color="Green" Offset="0.44"/>
<GradientStop Color="Black" Offset="0.727"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="gs2" EndPoint="0.5,-0.3" StartPoint="0.5,0.8">
<GradientStop Color="White" Offset="0.44"/>
<GradientStop Color="Black" Offset="0.727"/>
</LinearGradientBrush>
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Path x:Name="path1" ........... Fill="{StaticResource gs1}">
</Path>
<ContentPresenter .........../>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="path1" Property="Fill" Value="{StaticResource gs2}"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
它会为你工作。您可以使用数据绑定也,在code声明一个颜色的DependencyProperty,用渐变停止的色彩属性绑定它,只要你想更新它。
it will work for you. You can use DataBinding also, Declare a Color DependencyProperty in code, Bind it with the GradientStop's Color property and update it whenever you want.
这篇关于WPF风格:如何改变渐变停止颜色触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!