WPF:绑定更改时,如何激发一个EventTrigger(或动画)?
-
21-09-2019 - |
题
我们有,当切换按钮被选中和未选中运行(扩展了ListView的高度,然后折叠一个ListView的高度)一个简单的动画。你怎么火EventTrigger(或动画)的<Storyboard x:Key="CommentsCollapse">
当x:Name="DetailsGrid"
电网DataContext的绑定变化在以下XAML?
在换句话说,只要对“DetailsGrid”绑定的变化,我们希望被触发“CommentsCollapse”故事板,以确保ListView控件返回到其折叠状态。
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800"
Height="400">
<Page.Resources>
<Storyboard x:Key="CommentsExpand">
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="CommentsListView"
Storyboard.TargetProperty="(FrameworkElement.Height)">
<SplineDoubleKeyFrame KeyTime="00:00:00.200" Value="300"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="CommentsCollapse">
<DoubleAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="CommentsListView"
Storyboard.TargetProperty="(FrameworkElement.Height)">
<SplineDoubleKeyFrame KeyTime="00:00:00.200" Value="75"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Page.Resources>
<Page.Triggers>
<EventTrigger RoutedEvent="ToggleButton.Checked" SourceName="CommentsToggleButton">
<BeginStoryboard Storyboard="{StaticResource CommentsExpand}"/>
</EventTrigger>
<EventTrigger RoutedEvent="ToggleButton.Unchecked" SourceName="CommentsToggleButton">
<BeginStoryboard Storyboard="{StaticResource CommentsCollapse}"/>
</EventTrigger>
</Page.Triggers>
<Grid DataContext="{Binding Path=CurrentTask.Workflow.Invoice}" x:Name="DetailsGrid">
<StackPanel Orientation="Horizontal">
<Canvas Width="428">
<GroupBox Width="422" Margin="5,0,0,0">
<GroupBox.Header>
<StackPanel Orientation="Horizontal">
<ToggleButton
x:Name="CommentsToggleButton"
Width="20"
Height="10"
Margin="5,0,0,0">
<ToggleButton.Content>
<Rectangle
Width="5"
Height="5"
Fill="Red"/>
</ToggleButton.Content>
</ToggleButton>
<TextBlock Foreground="Blue" Text="Comments"/>
</StackPanel>
</GroupBox.Header>
<ListView
x:Name="CommentsListView"
Height="75"
ItemsSource="{Binding Path=Comments}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=Date}" Header="Date"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="User"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Description}" Header="Comment"/>
</GridView>
</ListView.View>
</ListView>
</GroupBox>
</Canvas>
</StackPanel>
</Grid>
</Page>
解决方案
我确实发现过,这是不可能的XAML。您需要的DataContextChanged仅事件,该事件是不是一个RoutedEvent,因此不能在一个EventTrigger使用。
这似乎工作,虽然:
<Window x:Class="DatacontextChangedSpike.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Storyboard x:Key="ListViewExpands" AutoReverse="True" RepeatBehavior="2x">
<DoubleAnimation Storyboard.TargetName="PulsingListView" Storyboard.TargetProperty="Height"
From="10" To="60"/>
</Storyboard>
</Window.Resources>
<StackPanel>
<ListView Name="PulsingListView" BorderThickness="2" BorderBrush="Black"
DataContextChanged="PulsingListView_DataContextChanged">
<TextBlock>Listview</TextBlock>
</ListView>
<Button Click="Button_Click" >Change DataContext</Button>
</StackPanel>
</Window>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DatacontextChangedSpike
{
public partial class Window1 : Window
{
public Window1()
{
DataContext = new List<string>();
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
DataContext = new List<int>();
}
private void PulsingListView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var sb = (Storyboard)FindResource("ListViewExpands");
sb.Begin();
}
}
}
其他提示
您可以触发从DataTrigger(EnterAction和ExitAction)故事板,如果当你改变的DataContext你可以设置一个视图模型属性为true或false。所以DataTrigger将根据新的布尔属性。
我不认为你可以通过纯XAML中做到这一点,你就必须做到这一点的代码(或更好,但写一个通用的表达行为,让您的可以的在描述它XAML)。检查你的依赖项属性的PropertyMetadata看你如何挂钩的属性更改事件。
不隶属于 StackOverflow