代码之家  ›  专栏  ›  技术社区  ›  Phil

并不是StackPanel的所有datatrigger都在工作

  •  0
  • Phil  · 技术社区  · 7 年前

    我有一个显示文本和图像的按钮:

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

    活动状态=0->红色/ 活动状态=1->蓝色/ 活动状态=2->蓝色闪烁(为此,我使用故事板和彩色动画)

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

    PresentationFramework.dll中发生类型为“System.InvalidOperationException”的未处理异常

    其他信息:后台属性未指向路径(0)中的dependencyobject。(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>
    

    致以最诚挚的问候 菲尔

    1 回复  |  直到 7 年前
        1
  •  1
  •   Clemens    7 年前

    当你直接设置 Background="Transparent" 在StackPanel上,它具有更高的 precedence

    除此之外,如果你想制作动画 Background.Color 您应该始终明确指定SolidColorBrushes,而不是预定义的笔刷,如 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>
    
    推荐文章