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>
它建立并运行,但不幸的是,触发器不会触发。我怀疑这与数据上下文有关,但我不确定。
可能有人开导我为我要去哪里错了
我已经添加了一个回调到依赖属性和“值”正在改变,但触发器没有触发。 –