2012-08-23 68 views
1

我想通過下面提供的類向wpf控件添加一些功能。如何將CommandParameter添加到文本

namespace ATCheckerView 
{ 
    public class AttachedMouseBinding 
    { 
     public static ICommand GetCommand(DependencyObject obj) 
     { 
      return (ICommand)obj.GetValue(CommandProperty); 
     } 

     public static void SetCommand(DependencyObject obj, Object value) 
     { 
      obj.SetValue(CommandProperty, value); 
     } 

     public static Object GetCommandParameter(DependencyObject obj) 
     { 
      return (ICommand)obj.GetValue(CommandParameterProperty); 
     } 

     public static void SetCommandParameter(DependencyObject obj, Object value) 
     { 
      obj.SetValue(CommandParameterProperty, value); 
     } 

     public static readonly DependencyProperty CommandProperty = 
      DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(AttachedMouseBinding), 
      new FrameworkPropertyMetadata(CommandChanged)); 

     public static readonly DependencyProperty CommandParameterProperty = 
      DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(AttachedMouseBinding), 
      new FrameworkPropertyMetadata(CommandParameterChanged)); 

     private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      var fe = d as FrameworkElement; 
      var command = e.NewValue as ICommand; 
      if (command == null) return; 
      var inputBinding = new InputBinding(command, new MouseGesture(MouseAction.LeftDoubleClick)); 
      fe.InputBindings.Add(inputBinding); 
     } 

     private static void CommandParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      //var fe = d as FrameworkElement; 
      // var parameter = e.NewValue as Object; 
     } 
    } 
} 

我用這樣的:

<TextBlock Grid.Column="0" HorizontalAlignment="Stretch" 
      Tag="{Binding ElementName=Root, Path=DataContext}" 
      Text="{Binding Path=path, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
      ContextMenu="{StaticResource SchemeContextMenu}" 
      local:AttachedMouseBinding.Command="{Binding ElementName=Root, Path=DataContext.vclient.OpenInViewer}" 
      local:AttachedMouseBinding.CommandParameter="{Binding}"> 
</TextBlock> 

我的問題是:因爲現在送CommandParameter到命令

UPD: 伊戈爾的變異與我的變化:

 
private static void OnMouseLeftClick(object sender, RoutedEventArgs e) 
     { 
      var me = e as MouseButtonEventArgs; 
      if (me != null && me.ClickCount != 2) return;

FrameworkElement control = sender as FrameworkElement; ICommand command = (ICommand)control.GetValue(CommandProperty); object commandParameter = control.GetValue(CommandParameterProperty); if (command.CanExecute(commandParameter)) command.Execute(commandParameter); }

+0

其中'Command.Execute(對象)'應該是叫什麼?因爲它是唯一需要命令參數的地方,答案直接取決於它 – dvvrd

回答

2

你是否在mouseleft點擊中激發了命令?

試試這個:

public static class AttachedMouseBinding 
{ 


    public static DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(AttachedMouseBinding), new UIPropertyMetadata(CommandChanged)); 

    public static DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(AttachedMouseBinding), new UIPropertyMetadata(null)); 

    public static void SetCommand(DependencyObject target, ICommand value) 
    { 
     target.SetValue(CommandProperty, value); 
    } 

    public static void SetCommandParameter(DependencyObject target, object value) 
    { 
     target.SetValue(CommandParameterProperty, value); 
    } 
    public static object GetCommandParameter(DependencyObject target) 
    { 
     return target.GetValue(CommandParameterProperty); 
    } 

    private static void CommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) 
    { 
     FrameworkElement control = target as FrameworkElement; 
     if (control != null) 
     { 
      if ((e.NewValue != null) && (e.OldValue == null)) 
      { 
       control.PreviewMouseLeftButtonDown += OnMouseLeftClick; 
      } 
      else if ((e.NewValue == null) && (e.OldValue != null)) 
      { 
       control.PreviewMouseLeftButtonDown -= OnMouseLeftClick; 
      } 
     } 
    } 

    private static void OnMouseLeftClick(object sender, RoutedEventArgs e) 
    { 
     FrameworkElement control = sender as FrameworkElement; 
     ICommand command = (ICommand)control.GetValue(CommandProperty); 
     object commandParameter = control.GetValue(CommandParameterProperty); 
     if (command.CanExecute(commandParameter)) 
      command.Execute(commandParameter); 
    } 


} 
+0

好的,它對我很有用。但我想通過雙擊鼠標左鍵來執行命令。我以這種方式得到了這個(添加到問題),這是正確的? – psct

+1

你的解決方案沒問題! –

1

修改XAML

<TextBlock Grid.Column="0" HorizontalAlignment="Stretch" 
      Tag="{Binding ElementName=Root, Path=DataContext}" 
      Text="{Binding Path=path, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
      ContextMenu="{StaticResource SchemeContextMenu}"> 
    <TextBlock.InputBindings> 
      <MouseBinding MouseAction="LeftDoubleClick" 
         Command="{Binding ElementName=Root, Path=DataContext.vclient.OpenInViewer}" 
         CommandParameter="{Binding}"/> 
    </TextBlock.InputBindings> 
</TextBlock> 
+0

In Net 3. *它不適合我,因爲綁定錯誤 – psct

+1

mmm,我使用Net 4進行測試。 –