我在屏幕上收集了错误,每个错误一行。用户可以通过单击该行上的按钮来关闭任何错误消息。代码示例:
<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>
问题是什么:正如你所看到的,我添加了故事板触发器以使其清晰,我希望平滑地隐藏消息,并且只有在关闭它之后。所以它将是第一个故事板,然后执行命令。如何实现?尽可能减少代码隐藏,请。
答案 0 :(得分:2)
您可以使用这样的附加属性:
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>
因此命令将在动画后触发。
答案 1 :(得分:0)
在DoubleAnimation中使用Completed事件处理程序
<Storyboard TargetProperty="Height" TargetName="grid">
<DoubleAnimation To="0" Duration="0:0:0.35" Completed="do_this"/>
</Storyboard>
您必须在后面的代码中设置事件,但对于您需要做的事情来说非常简单。
编辑:这是您应该在代码隐藏中获取的事件处理程序以及删除usercontrol所需的代码行
private void do_this(object sender, EventArgs e)
{
this.Grid.Children.Remove(UserControl);
}
你需要拥有ax:用户控件的名称和它所在的网格。我刚测试了它并且确实有效,我认为没有任何方法可以做到这一点触摸代码隐藏。