网格隐藏/显示动画

时间:2014-11-10 10:19:10

标签: wpf xaml animation triggers grid

我有一些用户控件,它是具有透明背景的网格,除此之外还有另一个网格,当触发触发时,它会改变其宽度(最终它将是一个输入/输出动画)。目的是简单地使用与Visibility不同的东西,这些东西在没有任何动画的情况下立即隐藏整个控件。这是它所基于的属性:

    public static readonly DependencyProperty IsOpenedProperty = DependencyProperty.Register ("IsOpened", typeof (Boolean), typeof (SidePanel), new PropertyMetadata (false));

    public bool IsOpened {
        get {
            if (GetValue (IsOpenedProperty) != null)
                return (bool)GetValue (IsOpenedProperty);
            else
                return false;
        }
        set {
            SetValue (IsOpenedProperty, value);
            Console.WriteLine(value);
        }
    }

网格本身:

<Grid>
    <Grid.Style>
        <Style TargetType="{x:Type Grid}">
            <Setter Property="Width" Value="50"/>
            <Setter Property="Background" Value="Gray"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsOpened}" Value="True">
                    <Setter Property="Width" Value="350"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsOpened}" Value="False">
                    <Setter Property="Width" Value="0"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Style>

    <TextBlock Text="{Binding ElementName=Side, Path=Width}"/>
</Grid>

控制台为我提供了核心输出,但触发器根本不会触发。

刚刚检查了一件事,当我为x设置触发器:Null值然后它一直被触发时,Get怎么可能不会在这里返回空值?

1 个答案:

答案 0 :(得分:0)

好吧,我刚刚放弃了依赖属性,创建了私有变量来保存值,并使用INotifyPropertyChanged接口实现添加了数据上下文,现在它可以正常工作。