首页 > 解决方案 > 并非所有 StackPanel 的 DataTriggers 都在工作

问题描述

我有一个显示文本和图像的按钮:为此,我使用带有 TextBlock 和 Image 的 StackPanel。

当变量“ActiveState”改变时,StackPanel 的背景也应该改变,为此我使用 DataTriggers

ActiveState=0 -> Red / ActiveState=1 -> Blue / ActiveState=2 -> 闪烁的蓝色(为此我使用情节提要和彩色动画)

闪烁的触发器 (Value=2) 工作正常,但其他两个触发器 (Value=0 + Value=1) 不工作。

当我删除 Stackpanel 的背景 (Background="Transparent") 时,前两个触发器正在工作,但最后一个触发器出现以下异常:

PresentationFramework.dll 中出现“System.InvalidOperationException”类型的未处理异常

附加信息:背景属性不指向路径“(0).(1)”中的依赖对象

这是我的代码:

<Button>
  <Button.Template>
    <ControlTemplate TargetType="Button">
        <StackPanel Orientation="Horizontal" Name="SelectButtonStackpanel" Background="Transparent">
            <TextBlock Text="{Binding Text}"/>
            <Image Source="{Binding Image}" Stretch="Uniform" Height="40" Width="40"/>                    
                <StackPanel.Style>
                    <Style TargetType="{x:Type StackPanel}">
                        <Style.Triggers>

                            <DataTrigger Binding="{Binding ActiveState}" Value="0">
                                <Setter Property="Background" Value="Red"/>
                            </DataTrigger>

                            <DataTrigger Binding="{Binding ActiveState}" Value="1">
                                <Setter Property="Background" Value="Blue"/>
                            </DataTrigger>

                            <DataTrigger Binding="{Binding ActiveState}" Value="2">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimation 
                                                Storyboard.TargetProperty="(StackPanel.Background).(SolidColorBrush.Color)"
                                                To="Blue" Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever"
                                                >
                                            </ColorAnimation>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>

                                <DataTrigger.ExitActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimation 
                                                Storyboard.TargetProperty="(StackPanel.Background).(SolidColorBrush.Color)"
                                                Duration="0:0:1"
                                                >
                                            </ColorAnimation>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.ExitActions>
                            </DataTrigger>

                        </Style.Triggers>
                    </Style>
                </StackPanel.Style>
            </StackPanel>
    </ControlTemplate>
  </Button.Template>
</Button>

你知道我是如何让所有三个触发器工作的吗?

最好的问候菲尔

标签: wpfbackgroundstoryboarddatatriggerstackpanel

解决方案


当您直接Background="Transparent"在 StackPanel 上设置时,它的优先级高于从样式设置器中设置的值。所以删除直接分配并为默认背景添加另一个 Setter。

除此之外,如果您想制作动画Background.Color,您应该始终明确指定 SolidColorBrushes 而不是预定义的 Brushes,如Background="Red".

<StackPanel Orientation="Horizontal" Name="SelectButtonStackpanel">
    <StackPanel.Style>
        <Style TargetType="StackPanel">
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush Color="Transparent"/>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ActiveState}" Value="0">
                    <Setter Property="Background">
                        <Setter.Value>
                            <SolidColorBrush Color="Red"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding ActiveState}" Value="1">
                    <Setter Property="Background">
                        <Setter.Value>
                            <SolidColorBrush Color="Blue"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding ActiveState}" Value="2">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation 
                                    Storyboard.TargetProperty="Background.Color"
                                    To="Blue" Duration="0:0:1"
                                    AutoReverse="True" RepeatBehavior="Forever">
                                </ColorAnimation>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
</StackPanel>

推荐阅读