0


我正在尝试创建一个自定义控件(它只是一个从 Grid 派生的自定义网格,我想将它保留为一个 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>

当用户按住鼠标右键时,这个想法非常简单,我只是希望网格的大小增加。我知道动画有效,因为如果我从触发器“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);
        }    
    }
}
4

1 回答 1

0

xml:

<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);
        }
    }
}
于 2013-04-12T03:36:34.017 回答