gpt4 book ai didi

WPF:绑定(bind)更改时如何触发EventTrigger(或动画)?

转载 作者:行者123 更新时间:2023-12-02 09:00:17 25 4
gpt4 key购买 nike

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

换句话说,每当“DetailsGrid”的 Binding 发生变化时,我们希望触发“CommentsCollapse”StoryBoard 以确保 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();
}
}
}

关于WPF:绑定(bind)更改时如何触发EventTrigger(或动画)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1764867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com