WPF自定义控件EventTrigger未触发或动画不起作用

时间:2013-04-12 02:30:23

标签: wpf grid storyboard custom-controls eventtrigger


我正在创建一个自定义控件(它只是一个源自Grid的自定义网格,我想保持它是一个网格因为我想尝试的其他一些东西),我的问题是为什么不是故事板当我试图从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>

当用户按住鼠标右键时,我只想让网格增大,这个想法非常简单。我知道动画是有效的,因为如果我从Trigger“IsMouseOver”开始它可以正常工作,但是如果我按下鼠标就什么都没发生了 并非它不是评论问题:P

我的自定义控件源自网格。

已添加编辑代码

使用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);
        }    
    }
}

1 个答案:

答案 0 :(得分:0)

的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);
        }
    }
}