2016-08-12 95 views
0

我试图创建一个自定义的StackPanel,当视图模型上的属性发生变化时执行动画(缩放)。我在我的派生类wpf自定义堆栈面板数据触发器

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(); 
    } 
} 

创建一个依赖属性在我加入样式与结合依赖属性

<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> 

在我的测试窗口,我一个DataTrigger的XAML有

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

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

可能有人开导我为我要去哪里错了

+0

我已经添加了一个回调到依赖属性和“值”正在改变,但触发器没有触发。 –

回答

0

多摆弄我发现我需要使用

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

不幸的是,我在创造一个真正的可配置的StackPanel得到更进一步后与规模动画作为一个跑入

不能冻结这个故事板时间表树使用跨线程

当您尝试绑定DoubleAnimation的“To”属性时。这意味着我无法在设计时使用依赖属性来配置方向。如果有人知道通过这个方法,请让我知道!

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

如果有人知道更好的方法来做到这一点,请让我知道。这可能是我的做法是错误的?