首页 文章

wpf自定义stackpanel数据触发器

提问于
浏览
0

我正在尝试创建一个自定义StackPanel,当viewmodel上的属性发生更改时,它会执行动画(缩放) . 我在派生类中创建了一个Dependency Property

public partial class TrgScaleStackPanel : StackPanel
{
    #region TriggerValue DP

    /// <summary>
    /// Gets or sets the Value which is being displayed
    /// </summary>
    public bool TriggerValue
    {
        get { return (bool)GetValue(TriggerValueProperty); }
        set { SetValue(TriggerValueProperty, value); }
    }

    /// <summary>
    /// Identified the Label dependency property
    /// </summary>
    public static readonly DependencyProperty TriggerValueProperty =
        DependencyProperty.Register("TriggerValue", typeof(bool),
          typeof(TrgScaleStackPanel), new PropertyMetadata(false));

    #endregion
    public TrgScaleStackPanel()
    {
        InitializeComponent();
    }
}

在XAML中,我添加了Style,其中包含一个绑定到依赖项属性的DataTrigger

<StackPanel x:Class="StackPanelDemo.TrgScaleStackPanel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         x:Name="TrgScaleParent"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<StackPanel.Style>
    <Style TargetType="{x:Type StackPanel}" >
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=TrgScaleParent.TriggerValue}" Value="True">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Duration="0:0:0.25" To="1" Storyboard.TargetProperty="(LayoutTransform).(ScaleTransform.ScaleY)"/>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Duration="0:0:0.25" To="0" Storyboard.TargetProperty="(LayoutTransform).(ScaleTransform.ScaleY)"/>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.ExitActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</StackPanel.Style>

在我的测试窗口我有

<stackPanelDemo:TrgScaleStackPanel TriggerValue="{Binding SettingsViewVisible}">
                <TextBlock>Hello</TextBlock>
                <Button>Goodbye</Button>
                <stackPanelDemo:TrgScaleStackPanel.LayoutTransform>
                    <ScaleTransform ScaleX="1" ScaleY="0" />
                </stackPanelDemo:TrgScaleStackPanel.LayoutTransform>
            </stackPanelDemo:TrgScaleStackPanel>

它构建并运行但不幸的是,触发器不会触发 . 我怀疑这与数据上下文有关,但我不太确定 .

有人可以告诉我哪里出错了

1 回答

  • 0

    经过多次摆弄后,我发现我需要使用它

    <DataTrigger Binding="{Binding Path=TriggerValue, RelativeSource={RelativeSource Self}}" Value="True">
    

    不幸的是,我已经进一步创建了一个真正可配置的带有缩放动画的StackPanel

    无法冻结此故事板时间轴树以供跨线程使用

    当一个人试图绑定DoubleAnimation的“To”属性时 . 这意味着我无法在设计时使用依赖项属性配置方向 . 如果有人知道如何通过这个,请告诉我!

    另一个有趣的问题是,除非我在使用自定义StackPanel时指定了LayoutTransform,否则动画根本不会触发 . 我不知道为什么?

    如果有人知道更好的方法,请告诉我 . 这可能是我的做法错了?

相关问题