我正在尝试通过下面的课程为wpf控件添加一些功能。
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);
}
FrameworkElement control = sender as FrameworkElement;
ICommand command = (ICommand)control.GetValue(CommandProperty);
object commandParameter = control.GetValue(CommandParameterProperty);
if (command.CanExecute(commandParameter))
command.Execute(commandParameter);
}
答案 0 :(得分:2)
您是否在鼠标左键单击时尝试触发命令?
试试这个:
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);
}
}
答案 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>