WPF样式:如何在触发器中更改GradientStop颜色

时间:2010-03-21 16:38:33

标签: wpf button triggers styles

我有一个按钮样式:

<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。如何在Trigger中访问Color Property?我尝试了TargetName =“gs1”和TargetName =“path1.gs1”,但它不起作用。有什么想法吗?

2 个答案:

答案 0 :(得分:7)

试试这个:

<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属性绑定它,并随时更新它。

答案 1 :(得分:2)

我认为你将不得不更换整个画笔。造型按钮Here is a good example

<ControlTemplate.Triggers>
   <Trigger Property="IsMouseOver" Value="True">
      <Setter Property="Fill" TargetName="path1">
         <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,-0.3" StartPoint="0.5,0.8">
               <GradientStop Color="Black" Offset="0.44"/>
               <GradientStop Color="Green" Offset="0.727"/>
            </LinearGradientBrush>
         </Setter.Value>
       </Setter>
    </Trigger>
</ControlTemplate.Triggers>