找不到Trigger目标'stateBrush'

时间:2009-08-27 14:09:11

标签: wpf triggers controltemplate datatrigger

以下ControlTemplate有什么问题,为什么找不到名为Brush?我总是得到错误找不到触发器目标'stateBrush'。在我的模板中,当状态更改为WaitForActive时,我将开始为背景设置动画,并且当状态更改为Active时,我想将其设置为绿色。很简单。我的方法有些不确定吗?

当状态发生变化时,真的有必要删除旧故事板。经过大量的状态改变后会不会创建很多故事板?

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