views:

251

answers:

1

What is wrong with the following ControlTemplate why can't it find the named Brush? I alway get the error Cannot find the Trigger target 'stateBrush'. In my template I would start to animate the background when the state changes to WaitForActive and I want to set it to green when the state changes to Active. Pretty simple. Is my approach somehow flawed?

Also is it really necessary to remove the old storyboard when the state changes. After a lot of State changes won't there be a lot of storyboards created?

<Style TargetType="{x:Type local:GateControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:GateControl}">
                        <Grid ClipToBounds="True" x:Name="rootLayout">
                            <Grid.Background>
                                <SolidColorBrush x:Name="stateBrush"/>
                            </Grid.Background>
                            <Rectangle Stroke="{TemplateBinding StrokeBrush}"
                                       StrokeThickness="{TemplateBinding StrokeThickness}">
                                <Rectangle.Fill>
                                    <LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
                                        <GradientStop Color="#AA111111" Offset="0"/>
                                        <GradientStop Color="#AA111111" Offset="1"/>
                                        <GradientStop Color="Transparent" Offset="0.5"/>
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <local:Cross StrokeBrush="{TemplateBinding StrokeBrush}" 
                                     StrokeThickness="{TemplateBinding StrokeThickness}" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path=State, RelativeSource={RelativeSource Self}}" Value="WaitForActive">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard x:Name="waitForActiveStoryboard">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="stateBrush" Storyboard.TargetProperty="Color"
                                                FillBehavior="HoldEnd" 
                                                RepeatBehavior="Forever"
                                                AutoReverse="True">
                                                <ColorAnimationUsingKeyFrames.KeyFrames>
                                                    <LinearColorKeyFrame KeyTime="00:00:00" Value="Green"/>
                                                    <LinearColorKeyFrame KeyTime="00:00:0.25" Value="Green"/>
                                                    <LinearColorKeyFrame KeyTime="00:00:0.5" Value="Yellow" />
                                                    <LinearColorKeyFrame KeyTime="00:00:0.75" Value="Yellow" />
                                                </ColorAnimationUsingKeyFrames.KeyFrames>
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                                <DataTrigger.ExitActions>
                                    <RemoveStoryboard BeginStoryboardName="waitForActiveStoryboard" />
                                </DataTrigger.ExitActions>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=State, RelativeSource={RelativeSource Self}}" Value="Active">
                                <Setter TargetName="stateBrush" Property="Color" Value="Green" />
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
A: 

See http://blogs.msdn.com/mikehillberg/archive/2007/05/07/HowToTargetATemplateSetterAtNonElementContent.aspx

MarkS
I already use the Tag Property for something else :(
bitbonk
To solve that I can simply add a custom attached property to the element. **This is generally a better idea than to use the Tag property**
bitbonk