如何通过WPF中的事件触发嵌套元素动画?

时间:2014-01-01 08:30:07

标签: c# wpf events routed-events

我有这个配置:

UserControl
- Grid
- - Grid
- - - Grid.Triggers
- - - - EventTrigger (this is where I want to listen for an event on UserControl)
- - ...etc

我正在尝试使用此文档创建一个路由事件(也许这是我的方向错误?):http://msdn.microsoft.com/en-us/library/ms752288(v=vs.110).aspx

到目前为止,我有:

#region Show Blackout Event
public static readonly RoutedEvent ShowBlackoutEvent =
    EventManager.RegisterRoutedEvent("ShowBlackout", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(PatchManagerView));

// Provide CLR accessors for the event 
public event RoutedEventHandler ShowBlackout
{
    add { AddHandler(ShowBlackoutEvent, value); }
    remove { RemoveHandler(ShowBlackoutEvent, value); }
}

void RaiseShowBlackout()
{
    Dispatcher.Invoke(() => RaiseEvent(new RoutedEventArgs(ShowBlackoutEvent)));
}
#endregion

我想要的是,当引发此事件时,会满足事件触发条件并播放动画。

编辑 - 工作代码:

XAML

<UserControl x:Class="uPatch.Views.PatchManagerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:uPatch"
             xmlns:views="clr-namespace:uPatch.Views"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d" 
             d:DesignHeight="800" d:DesignWidth="600">
    <UserControl.Resources>
        <!--<Storyboard  x:Key="ShowBlurAnimation">
            <DoubleAnimation Storyboard.TargetProperty="Radius" From="0" To="15" Duration="0:0:.25" />
        </Storyboard>-->
        <Storyboard x:Key="ShowBlackoutAnimation">
            <DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:.25" />
        </Storyboard>
        <Storyboard x:Key="HideBlackoutAnimation">
            <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:.25" />
        </Storyboard>
        <Storyboard x:Key="ShowPatchAnimation">
            <DoubleAnimation Storyboard.TargetProperty="Width" From="1" To="480" Duration="0:0:.25" />
            <DoubleAnimation Storyboard.TargetProperty="Height" From="1" To="320" Duration="0:0:.25" />
        </Storyboard>
        <Storyboard x:Key="HidePatchAnimation">
            <DoubleAnimation Storyboard.TargetProperty="Width" From="480" To="1" Duration="0:0:.25" />
            <DoubleAnimation Storyboard.TargetProperty="Height" From="320" To="1" Duration="0:0:.25" />
        </Storyboard>
    </UserControl.Resources>
    <Grid>
        <Grid x:Name="DimableGrid">
            <!--<Grid.Effect>
                <BlurEffect x:Name="blurEffect">
                    <BlurEffect.Radius>
                        <MultiBinding>
                            <MultiBinding.Converter>
                                <local:MultiplyConverter />
                            </MultiBinding.Converter>
                            <Binding Path="DataContext.DimAmount" RelativeSource="{RelativeSource AncestorType={x:Type UserControl}}" />
                            <Binding Path="Tag" ElementName="DimableGrid" />
                        </MultiBinding>
                    </BlurEffect.Radius>
                </BlurEffect>
            </Grid.Effect>-->
            <Grid x:Name="AllPatches">
                <Grid.RowDefinitions>
                    <RowDefinition Height="35" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid Grid.Row="0" HorizontalAlignment="Left">
                    <StackPanel Orientation="Horizontal" local:MarginSetter.Margin="2">
                        <Button Content="Add New Patch" Width="100" Height="25"/>
                        <Button Content="Add New Patch" Width="100" Height="25"/>
                    </StackPanel>
                </Grid>
                <Grid Grid.Row="1">
                    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" >
                        <ItemsControl AlternationCount="2" ItemsSource="{Binding PatchManager.Patches}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Vertical"   />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Grid x:Name="RowItem">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="75" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Grid.Column="0" Margin="5,0,0,0" VerticalAlignment="Center" Text="{Binding Version}" />
                                        <Button Grid.Column="2" Content="View" 
                                            Command="{Binding Path=DataContext.ViewPatch, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
                                            CommandParameter="{Binding}" />
                                    </Grid>
                                    <DataTemplate.Triggers>
                                        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                                            <Setter Property="Background" Value="Gray" TargetName="RowItem"/>
                                        </Trigger>
                                        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                            <Setter Property="Background" Value="{DynamicResource WindowBackgroundBrush}" TargetName="RowItem"/>
                                        </Trigger>
                                        <Trigger Property="ItemsControl.IsMouseOver" Value="True">
                                            <Setter Property="Border.Background" Value="{DynamicResource FocusBrush}" TargetName="RowItem"/>
                                        </Trigger>
                                    </DataTemplate.Triggers>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </ScrollViewer>
                </Grid>
            </Grid>

            <Grid x:Name="Blackout">
                <Grid.Triggers>
                    <EventTrigger RoutedEvent="views:PatchManagerView.ShowPatch">
                        <BeginStoryboard Storyboard="{StaticResource ShowBlackoutAnimation}" />
                    </EventTrigger>
                    <EventTrigger RoutedEvent="views:PatchManagerView.HidePatch">
                        <BeginStoryboard Storyboard="{StaticResource HideBlackoutAnimation}" />
                    </EventTrigger>
                </Grid.Triggers>
                <Grid.Style>
                    <Style TargetType="Grid">
                        <Setter Property="Background" Value="#aa000000" />
                        <Setter Property="Visibility" Value="Hidden" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=DataContext.IsShowingPatch, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Value="True" >
                                <Setter Property="Visibility" Value="Visible" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Grid.Style>
            </Grid>
        </Grid>

        <Grid x:Name="PatchDisplay"
              local:StoryboardExtensions.Storyboard="{StaticResource HidePatchAnimation}"
              local:StoryboardExtensions.Completed="{Binding HidePatchAnimationCompleted}">
            <Grid.Triggers>
                <EventTrigger RoutedEvent="views:PatchManagerView.ShowPatch">
                    <BeginStoryboard Storyboard="{StaticResource ShowPatchAnimation}" />
                </EventTrigger>
                <EventTrigger RoutedEvent="views:PatchManagerView.HidePatch">
                    <BeginStoryboard Storyboard="{StaticResource HidePatchAnimation}" />
                </EventTrigger>
            </Grid.Triggers>
            <Grid.Style>
                <Style TargetType="Grid">
                    <Setter Property="Visibility" Value="Hidden" />
                    <Setter Property="Width" Value="1" />
                    <Setter Property="Height" Value="1" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsShowingPatch}" Value="True" >
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Grid.Style>

            <Border Background="{DynamicResource TabControlContentBrush}" BorderThickness="1" CornerRadius="10">
                <Grid Margin="10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <views:PatchView Grid.Row="0" Grid.ColumnSpan="2" DataContext="{Binding SelectedPatch}" Margin="5" />
                    <Button Command="{Binding ClosePatchView}" Margin="5" Width="100" Height="25" Grid.Row="1" Grid.Column="2" Content="Close" />
                </Grid>
            </Border>
        </Grid>
    </Grid>
</UserControl>

代码隐藏

using System;
using System.Windows;
using System.Windows.Controls;
using uPatch.ViewModels;

namespace uPatch.Views
{
    /// <summary>
    /// Interaction logic for PatchManagerView.xaml
    /// </summary>
    public partial class PatchManagerView : UserControl
    {
        public PatchManagerView()
        {
            InitializeComponent();
        }

        public PatchManagerView(PatchManagerVM vm)
            : this()
        {
            vm.ShowPatch = () => RaiseShowPatch();
            vm.HidePatch = () => RaiseHidePatch();
            DataContext = vm;
        }

        #region Show Blackout Event
        public static readonly RoutedEvent ShowPatchEvent =
            EventManager.RegisterRoutedEvent("ShowPatch", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(PatchManagerView));

        // Provide CLR accessors for the event 
        public event RoutedEventHandler ShowPatch
        {
            add { AddHandler(ShowPatchEvent, value); }
            remove { RemoveHandler(ShowPatchEvent, value); }
        }

        void RaiseShowPatch()
        {
            Blackout.RaiseEvent(new RoutedEventArgs(ShowPatchEvent));
            PatchDisplay.RaiseEvent(new RoutedEventArgs(ShowPatchEvent));
        }
        #endregion

        #region Hide Blackout Event
        public static readonly RoutedEvent HidePatchEvent =
            EventManager.RegisterRoutedEvent("HidePatch", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(PatchManagerView));

        // Provide CLR accessors for the event 
        public event RoutedEventHandler HidePatch
        {
            add { AddHandler(HidePatchEvent, value); }
            remove { RemoveHandler(HidePatchEvent, value); }
        }

        void RaiseHidePatch()
        {
            Blackout.RaiseEvent(new RoutedEventArgs(HidePatchEvent));
            PatchDisplay.RaiseEvent(new RoutedEventArgs(HidePatchEvent));
        }
        #endregion

    }
}

所以看起来解决方案是在具有EventTrigger的元素上调用RaiseEvent(传递由顶级控件公开的RoutedEvent)。为此,我必须通过x:Name属性公开每个控件,并为每个控件引发一次事件。我没有看到与Bubble或Direct或Tunnel有任何区别......所以我对WPF使用的事件结构仍然有点困惑。

我更喜欢EventTriggers监听顶级事件而顶级控件可以是发布者......我需要为每个需要为该RoutedEvent引发EventTrigger的控件发布一次,这似乎很奇怪。

仍然试图弄清楚WPF的做事方式。

1 个答案:

答案 0 :(得分:2)

首先,如果您希望将路由事件隧道传输到网格,则需要将路由事件创建为attached event

public static readonly RoutedEvent ShowBlackoutEvent =
    EventManager.RegisterRoutedEvent("ShowBlackout", RoutingStrategy.Bubble, 
                              typeof(RoutedEventHandler), typeof(MainWindow));

public static void AddShowBlackoutHandler(DependencyObject d,
                                          RoutedEventHandler handler)
{
    UIElement uie = d as UIElement;
    if (uie != null)
    {
       uie.AddHandler(MainWindow.ShowBlackoutEvent, handler);
    }
}

public static void RemoveShowBlackoutHandler(DependencyObject d,
                                             RoutedEventHandler handler)
{
   UIElement uie = d as UIElement;
   if (uie != null)
   {
      uie.RemoveHandler(MainWindow.ShowBlackoutEvent, handler);
   }
}

此外,由于它是隧道事件,您需要从UIElement引发需要处理的事件。在你的情况下,它将是网格。

x:Name提供给您的网格:

UserControl
- Grid
- - Grid x:Name="grid"
- - - Grid.Triggers
- - - - EventTrigger

从后面的代码中引发一个事件:

void RaiseShowBlackout()
{
   grid.RaiseEvent(new RoutedEventArgs(ShowBlackoutEvent));
}

您已将事件声明为隧道事件,因此它将从根(即UserControl)隧道传输,并将一直持续到发件人,即gridEvenTrigger将被触发。