gpt4 book ai didi

WPF 样式 : how to change GradientStop Color in Trigger

转载 作者:行者123 更新时间:2023-12-04 11:31:54 25 4
gpt4 key购买 nike

我有一个按钮样式:

<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:Name="gs1"更改 GradientStop 的颜色,所以我使用 Trigger IsMouseOver。如何访问触发器中的颜色属性?我试过 TargetName="gs1"和 TargetName="path1.gs1"但它不起作用。任何的想法?

最佳答案

试试这个:

<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>

它会为你工作。您也可以使用 DataBinding,在代码中声明一个 Color DependencyProperty,将其与 GradientStop 的 Color 属性绑定(bind)并随时更新。

关于WPF 样式 : how to change GradientStop Color in Trigger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2487814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com