WPF:绑定更改时如何触发EventTrigger(或动画)? [英] WPF: how to fire an EventTrigger (or Animation) when binding changes?

查看:282
本文介绍了WPF:绑定更改时如何触发EventTrigger(或动画)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个简单的动画,当一个ToggleButton被检查并取消选中(扩展ListView的高度,然后折叠ListView的高度)运行。 DataContext Binding在中更改时,如何触发< Storyboard x:Key =CommentsCollapse> 的EventTrigger(或动画) x:Name =DetailsGrid以下XAML中的网格?

We have a simple animation that runs when a ToggleButton is checked and unchecked (expands a ListView's height and then collapses a ListView's height). How do you fire the EventTrigger (or Animation) for the <Storyboard x:Key="CommentsCollapse"> when the DataContext Binding changes in the x:Name="DetailsGrid" Grid in the following XAML?

换句话说,每当Binding对DetailsGrid进行更改时,希望CommentsCollapseStoryBoard被触发以确保ListView返回到其折叠状态。

In other words, whenever the Binding changes for the "DetailsGrid", we want the "CommentsCollapse" StoryBoard to be triggered to ensure the ListView is returned to its collapsed state.

<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中使用。

I do find too that this is not possible in XAML. You need the DataContextChanged event, which is not a RoutedEvent and thus cannot be used in an 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();
        }
    }
}

这篇关于WPF:绑定更改时如何触发EventTrigger(或动画)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆