WPF自定义控件事件触发器不触发或动画不工作




我正在尝试创建一个自定义控件(它只是一个自定义网格派生,我想保持它的网格,因为我想尝试的其他一些事情),我的问题是为什么当我试图从EventTrigger触发它时,故事板不起作用,但在正常触发下工作正常,我的代码如下:

<Style TargetType="{x:Type local:GridEx}">
    <Setter Property="Width" Value="100" />
    <Setter Property="Height" Value="100" />
    <Style.Triggers>
        <EventTrigger RoutedEvent="MouseRightButtonDown">
            <BeginStoryboard>
                <Storyboard >
                    <DoubleAnimation Storyboard.TargetProperty="Width" To="300" Duration="0:0:1" FillBehavior="HoldEnd" />
                    <DoubleAnimation Storyboard.TargetProperty="Height" To="300" Duration="0:0:1" FillBehavior="HoldEnd" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <Trigger Property="IsMouseOver" Value="True" >
            <!--<Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard >
                        <DoubleAnimation Storyboard.TargetProperty="Width" To="300" Duration="0:0:1" FillBehavior="HoldEnd" />
                        <DoubleAnimation Storyboard.TargetProperty="Height" To="300" Duration="0:0:1" FillBehavior="HoldEnd" />
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
            <Trigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard >
                        <DoubleAnimation Storyboard.TargetProperty="Width" To="100" Duration="0:0:1" FillBehavior="HoldEnd" />
                        <DoubleAnimation Storyboard.TargetProperty="Height" To="100" Duration="0:0:1" FillBehavior="HoldEnd" />
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.ExitActions>-->
            <Setter Property="Effect" Value="{StaticResource LiftEffect}"/>
            <Setter Property="RenderTransform">
                <Setter.Value>
                    <TranslateTransform X="-1" Y="-1" />
                </Setter.Value>
            </Setter>
            <Setter Property="Panel.ZIndex" Value="10" />
        </Trigger>
    </Style.Triggers>
</Style>

这个想法很简单,当用户按住鼠标右键时,我只是想让网格的大小变大。我知道动画是有效的,因为如果我从触发"IsMouseOver"开始,它会工作得很好,但如果我按住鼠标,什么也不会发生?
不,这不是评论的问题:P

My Custom Control from Grid.

EDIT CODE ADDED

使用System.Windows

;使用System.Windows.Controls;

namespace WPFCCLIB
{
    public class GridEx : Grid
    {
        public static readonly RoutedEvent LeftMouseDoubleClickEvent = EventManager.RegisterRoutedEvent("LeftMouseDoubleClick",RoutingStrategy.Bubble,typeof(RoutedEventHandler), typeof(GridEx));
        public event RoutedEventHandler LeftMouseDoubleClick
        {
            add { AddHandler(LeftMouseDoubleClickEvent, value); }
            remove { RemoveHandler(LeftMouseDoubleClickEvent, value); }
        }
        static GridEx()
        {
           DefaultStyleKeyProperty.OverrideMetadata(typeof(GridEx), new FrameworkPropertyMetadata(typeof(GridEx)));
        }
        void RaiseLeftMouseDoubleClickEvent()
        {
            RoutedEventArgs newEventArgs = new     RoutedEventArgs(GridEx.LeftMouseDoubleClickEvent);
            RaiseEvent(newEventArgs);
        }    
    }
}

Xaml:

<Window x:Class="WpfApplication10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication10"
        Title="MainWindow" Height="325" Width="422" Name="UI">
    <Window.Resources>
        <Style TargetType="{x:Type local:GridEX}">
            <Setter Property="Width" Value="100" />
            <Setter Property="Height" Value="100" />
            <Style.Triggers>
                <EventTrigger RoutedEvent="MouseRightButtonDown">
                    <BeginStoryboard>
                        <Storyboard >
                            <DoubleAnimation Storyboard.TargetProperty="Width" To="300" Duration="0:0:1" FillBehavior="HoldEnd" />
                            <DoubleAnimation Storyboard.TargetProperty="Height" To="300" Duration="0:0:1" FillBehavior="HoldEnd" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <local:GridEX Background="Blue" />
</Window>
代码:

namespace WpfApplication10
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    public class GridEX : Grid
    {
        public static readonly RoutedEvent LeftMouseDoubleClickEvent = EventManager.RegisterRoutedEvent("LeftMouseDoubleClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(GridEX));
        public event RoutedEventHandler LeftMouseDoubleClick
        {
            add { AddHandler(LeftMouseDoubleClickEvent, value); }
            remove { RemoveHandler(LeftMouseDoubleClickEvent, value); }
        }
        static GridEX()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(GridEX), new FrameworkPropertyMetadata(typeof(GridEX)));
        }
        void RaiseLeftMouseDoubleClickEvent()
        {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(GridEX.LeftMouseDoubleClickEvent);
            RaiseEvent(newEventArgs);
        }
    }
}

最新更新