2011-08-23 55 views
0

我想在我的樣式中添加一個事件觸發器,但由於某種原因它不起作用。 它給我一個錯誤{「值不能爲空。\ r \ nParameter name:routedEvent」} 我的目標是在我的控件的KeyDown事件中添加一個修復邏輯,任何人都知道發生了什麼?Xaml中的事件觸發器

下面的樣式在我的Theme.xaml中。

<Style.Triggers> 
      <EventTrigger> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="KeyDown"> 
        <Behaviors:ExecuteCommandAction Command="{Binding TabKeyDownCommand}" /> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
     </EventTrigger> 
    </Style.Triggers> 

這裏是我ExecuteCommandAction類:

/// <summary> 
/// Behaviour helps to bind any RoutedEvent of UIElement to Command. 
/// </summary> 
[DefaultTrigger(typeof (UIElement), typeof (EventTrigger), "KeyDown")] 
public class ExecuteCommandAction : TargetedTriggerAction<UIElement> 
{ 
    /// <summary> 
    /// Dependency property represents the Command of the behaviour. 
    /// </summary> 
    public static readonly DependencyProperty CommandParameterProperty = 
     DependencyProperty.RegisterAttached("CommandParameter", 
              typeof (object), 
              typeof (ExecuteCommandAction), 
              new FrameworkPropertyMetadata(null)); 

    /// <summary> 
    /// Dependency property represents the Command parameter of the behaviour. 
    /// </summary> 
    public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", 
                            typeof (ICommand 
                             ), 
                            typeof (
                             ExecuteCommandAction 
                             ), 
                            new FrameworkPropertyMetadata 
                             (null)); 

    /// <summary> 
    /// Gets or sets the Commmand. 
    /// </summary> 
    public ICommand Command 
    { 
     get { return (ICommand) GetValue(CommandProperty); } 
     set { SetValue(CommandProperty, value); } 
    } 

    /// <summary> 
    /// Gets or sets the CommandParameter. 
    /// </summary> 
    public object CommandParameter 
    { 
     get { return GetValue(CommandParameterProperty); } 
     set { SetValue(CommandParameterProperty, value); } 
    } 


    /// <summary> 
    /// Invokes the action. 
    /// </summary> 
    /// <param name="parameter">The parameter to the action. If the action does not require a parameter, the parameter may be set to a null reference.</param> 
    protected override void Invoke(object parameter) 
    { 
     if (Command != null) 
     { 
      if (Command.CanExecute(CommandParameter)) 
      { 
       Command.Execute(CommandParameter); 
      } 
     } 
    } 
} 

回答

2

Interactivity功能不能像這樣被使用,它不屬於正常TriggersEventTriggers甚至自己的風格。您可以使用它的元素是這樣的:

<Button> 
    <i:Interaction.Triggers> 
     <!-- ... --> 
    </i:Interaction.Triggers> 
</Button> 

錯誤是由在圍繞EventTrigger不設置RoutedEvent造成的,但即使你沒有設置它,新的錯誤會跟隨。

+0

噢好吧..你有沒有其他建議如何在KeyDown事件中添加修復邏輯而無需創建自定義控件? – maridob