views:

1115

answers:

1

Here is my XAML for a TabItem. I want to be able to set the Color of a single gradient stop in a trigger. I know that I can re-define the gradient completely in the trigger's setter, but I want to access a specific property on the background so I can animate it in the future.

I have tried every variation of everything in the trigger's setter and googled for a long time - but I still can't get it to compile. I have also tried class.property syntax, but still nothing. The current error this code raises is:

"Type 'Background.GradientStops[0]' was not found."

I am pretty sure I know what is going on here - and perhaps what I want is impossible. But there has to be a way to animate a control's gradient in a control template...

Can anyone help me? thanks

<Style TargetType="{x:Type TabItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <TextBlock Padding="6 2 6 2" Name="TheHeader">
                    <TextBlock.Background>
                        <LinearGradientBrush StartPoint="0, 0" EndPoint="0, 1">
                            <GradientStop Offset="0" Color="#f4fafd" />
                            <GradientStop Offset="1" Color="#ceedfa" />
                        </LinearGradientBrush>
                    </TextBlock.Background>
                    <ContentPresenter ContentSource="Header" Margin="0" />
                </TextBlock>
                <ControlTemplate.Triggers >
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="TheHeader" Property="Background.GradientStops[0].Color" Value="White" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
+1  A: 

You can animate it, like in the example here.

You also could use a slight hack to set it, though I always prefer creating multiple brushes as resources and swapping them or re-creating a brush in the as you mentioned.

<Style TargetType="{x:Type TabItem}">
  <Setter Property="Template">
   <Setter.Value>
    <ControlTemplate TargetType="{x:Type TabItem}">
     <TextBlock Padding="6 2 6 2"
          Name="TheHeader" Tag="#f4fafd">
                                    <TextBlock.Background>
                                            <LinearGradientBrush StartPoint="0, 0"
                 EndPoint="0, 1">
                                                    <GradientStop Offset="0" 
                  Color="{Binding ElementName=TheHeader, Path=Tag}"/>
                                                    <GradientStop Offset="1"
                  Color="#ceedfa" />
                                            </LinearGradientBrush>
                                    </TextBlock.Background>
                                    <ContentPresenter ContentSource="Header"
               Margin="0" />
     </TextBlock>
     <ControlTemplate.Triggers>
      <Trigger Property="IsSelected"
         Value="True">
       <Setter TargetName="TheHeader"
         Property="Tag"
         Value="Red" />
      </Trigger>
     </ControlTemplate.Triggers>
    </ControlTemplate>
   </Setter.Value>
  </Setter>
 </Style>
rmoore
your right, that is hacky. But if you can animate it by assigning the stops Names, why can't you do that in a controltemplate?
nlaq