2013-04-12 68 views
0


我正在創建一個自定義控件(它只是一個從網格派生的自定義網格,我想保持它爲一個網格,因爲我還有其他一些東西想嘗試一下),我的問題是,爲什麼沒有當我試圖從一個EventTrigger觸發它的故事板的工作,但一個正常的觸發下工作正常,我的代碼如下: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

我的自定義控件派生自網格。

編輯代碼使用System.Windows ADDED

; using 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); 
     }  
    } 
} 
+0

動畫工作正常標準的'Grid'是有什麼在你的派生電網與該鼠標事件interferring?你嘗試過'RoutedEvent =「Grid.MouseRightButtonDown」'? –

+0

據我所知不,可能會干擾我沒有明確添加任何內容:/但對於它在正常風格的網格中工作,我已經看到它的工作,這可能是爲什麼我有點困惑至於爲什麼它突然不起作用。 – Heinrich

+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); 
     } 
    } 
} 
+0

這確實有效,但僅僅是因爲樣式是在Windows.Resources中定義的,如果您要創建自定義控件並擁有Generic.xaml中的代碼,我認爲它不起作用:/ Mouse Over Trigger Fires而不是事件。代碼位於Generic.xaml文件中時。 – Heinrich

+0

然後這聽起來好像你沒有正確地將generic.xaml加載到應用程序資源中,Generic.xaml對任何其他ResourceDictionary的行爲都沒有任何不同 –

+0

是的,我在想,當我編寫最後一條評論時,唯一讓我不確定這個想法是這樣一個事實,即動畫在IsMouseOver觸發器下運行時可以正常工作,但我會查看一下:) – Heinrich