gpt4 book ai didi

c# - 动画后执行命令

转载 作者:行者123 更新时间:2023-12-03 10:35:48 28 4
gpt4 key购买 nike

我在屏幕上收集了错误,每个错误一行。用户可以通过单击该行上的按钮来关闭任何错误消息。代码示例:

<UserControl>
<ItemsControl ItemsSource="{Binding Errors}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid x:Name="grid" Height="20">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding ErrorText}"/>
<Button Grid.Column="1" Width="16" Height="16" Content="Close" Command="{Binding DataContext.RemoveErrorCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" CommandParameter="{Binding CurrentError}">
<Button.Triggers>
<EventTrigger RoutedEvent="ButtonBase.Click">
<BeginStoryboard>
<Storyboard TargetProperty="Height" TargetName="grid">
<DoubleAnimation To="0" Duration="0:0:0.35"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</UserControl>

有什么问题:如您所见,我在 Storyboard 中添加了触发器以使其清晰可见,我想平滑地隐藏消息,并且仅在关闭消息之后才可以。因此,将是第一个 Storyboard ,然后执行命令。如何实现?请尽量减少代码隐藏。

最佳答案

您可以使用以下附加属性:

public static class StoryboardHelper
{
public static readonly DependencyProperty CompletedCommandProperty = DependencyProperty.RegisterAttached("CompletedCommand", typeof(ICommand), typeof(StoryboardHelper), new PropertyMetadata(null, OnCompletedCommandChanged));
public static readonly DependencyProperty CompletedCommandParameterProperty = DependencyProperty.RegisterAttached("CompletedCommandParameter", typeof(object), typeof(StoryboardHelper), new PropertyMetadata(null));

public static void SetCompletedCommand(DependencyObject o, ICommand value)
{
o.SetValue(CompletedCommandProperty, value);
}

public static ICommand GetCompletedCommand(DependencyObject o)
{
return (ICommand)o.GetValue(CompletedCommandProperty);
}

public static void SetCompletedCommandParameter(DependencyObject o, object value)
{
o.SetValue(CompletedCommandParameterProperty, value);
}

public static object GetCompletedCommandParameter(DependencyObject o)
{
return o.GetValue(CompletedCommandParameterProperty);
}

private static void OnCompletedCommandChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var sb = sender as Storyboard;

if(sb != null)
{
sb.Completed += (a, b) =>
{
var command = GetCompletedCommand(sb);

if (command != null)
{
if (command.CanExecute(GetCompletedCommandParameter(sb)))
{
command.Execute(GetCompletedCommandParameter(sb));
}
}
};
}
}
}

并像这样使用它:
<Storyboard TargetProperty="Opacity" local:StoryboardHelper.CompletedCommand="{Binding Path=StoryCompletedCommand}">
<DoubleAnimation From="0" To="1" Duration="0:0:5"/>
</Storyboard>

因此,该命令将在动画之后触发。

关于c# - 动画后执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30030876/

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