首页 > 解决方案 > 没有设置器,DataTrigger EnterActions 不起作用?

问题描述

这里 WPF 中的情况真的很奇怪......假设我们在一个窗口中有这些代码:

<Window.Resources>
    <Storyboard x:Key="In" Storyboard.TargetProperty="Opacity">
        <DoubleAnimation Duration="0:0:1" To="1"/>
    </Storyboard>
    <Storyboard x:Key="Out" Storyboard.TargetProperty="Opacity">
        <DoubleAnimation Duration="0:0:1" To="0"/>
    </Storyboard>
    <Style x:Key="style" TargetType="FrameworkElement">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=MainWin, Path=Checked}" Value="True">
                <DataTrigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource Out}"/>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <BeginStoryboard Storyboard="{StaticResource In}"/>
                </DataTrigger.ExitActions>
                <!--<Setter Property="Tag" Value="{x:Null}"/>-->
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="TextBlock" BasedOn="{StaticResource style}"/>
    </StackPanel.Resources>

    <CheckBox Content="CheckBox bound to MainWindow.Checked property"
              IsChecked="{Binding ElementName=MainWin, Path=Checked}"/>
    <TextBlock Text="Tick above should hide me"/>
</StackPanel>

MainWindow.cs 中有一个依赖属性,名为Checked. 干净利落:

public bool Checked {
    get { return (bool)GetValue(CheckedProperty); }
    set { SetValue(CheckedProperty, value); }
}
public static readonly DependencyProperty CheckedProperty =
    DependencyProperty.Register("Checked", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));

基本上应该在选中TextBlock时隐藏。CheckBox但...

Setter除非我取消注释for ,否则上面的代码不起作用Tag(实际上不必是 Tag,但任何 Setter 都可以。)

如果我像这样直接设置它,它确实可以在不取消注释的情况下工作:StyleTextBlock

    <TextBlock Text="Tick above should hide me" Style="{StaticResource style}"/>

谁能解释这种奇怪的行为?我是否在框架中遇到某种错误?

标签: c#.netwpfstoryboarddatatrigger

解决方案


推荐阅读