2011-11-02 71 views
0

我在我的項目中有一個自定義的命令定義編譯好。當我嘗試從我的xaml視圖中使用它時,它不顯示。這是代碼。我在我的xaml中引用了我的本地項目,因此我不確定發生了什麼。棱鏡自定義命令不顯示在xaml

public class MouseDownCommandBehavior : CommandBehaviorBase<TextBox> 
{ 
    public MouseDownCommandBehavior(TextBox element) 
     : base(element) 
    { 
     element.MouseDown += new MouseButtonEventHandler(element_MouseDown); 
    } 

    private void element_MouseDown(object s, MouseEventArgs e) 
    { 
     base.ExecuteCommand(); 
    } 
} 
public static class MouseDown 
{ 
    public static readonly DependencyProperty BehaviorProperty = 
     DependencyProperty.RegisterAttached(
      "MouseDownCommandBehavior", 
      typeof(MouseDownCommandBehavior), 
      typeof(TextBox), 
      null); 

    public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
     "Command", 
     typeof(ICommand), 
     typeof(MouseDown), 
     new PropertyMetadata(OnSetCommand)); 

    public static readonly DependencyProperty CommandParameterProperty = 
     DependencyProperty.RegisterAttached(
      "CommandParameter", 
      typeof(object), 
      typeof(MouseDown), 
      new PropertyMetadata(OnSetParameter)); 

    private static void OnSetCommand(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
    { 
     TextBox element = dependencyObject as TextBox; 
     if (element != null) 
     { 
      MouseDownCommandBehavior behavior = GetOrCreateBehavior(element); 
      behavior.Command = e.NewValue as ICommand; 
     } 
    } 

    private static void OnSetParameter(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
    { 
     TextBox element = dependencyObject as TextBox; 
     if (element != null) 
     { 
      MouseDownCommandBehavior behavior = GetOrCreateBehavior(element); 
      behavior.CommandParameter = e.NewValue; 
     } 
    } 

    private static MouseDownCommandBehavior GetOrCreateBehavior(TextBox element) 
    { 
     MouseDownCommandBehavior behavior = element.GetValue(BehaviorProperty) as MouseDownCommandBehavior; 
     if (behavior == null) 
     { 
      behavior = new MouseDownCommandBehavior(element); 
      element.SetValue(BehaviorProperty, behavior); 
     } 

     return behavior; 
    } 
} 

回答

0

原來我錯過了幾種方法:GetCommand,set命令,SetCommandParameter,一個GetCommandParameter