将命令属性添加到任何自定义控件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将命令属性添加到任何自定义控件相关的知识,希望对你有一定的参考价值。
我可以使用以下代码导航到另一个视图。我想使用tabControl和自定义控件进行导航,但是没有用于绑定myCommand的Command属性。那么如何将我的命令绑定到自定义控件?:
<Button Command="{Binding myCommand}" Content="Nav"/>
无论如何要绑定? 2.如何将命令属性添加到自定义控件?
答案
CustomControls and UserControls
为命令创建DependencyProperty
。
public class MyControl : Control
{
static MyControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
}
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
"Command",
typeof(ICommand),
typeof(MyControl),
new PropertyMetadata(null, OnCommandPropertyChanged));
private static void OnCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MyControl control = d as MyControl;
if (control == null) return;
control.MouseLeftButtonDown -= OnControlLeftClick;
control.MouseLeftButtonDown += OnControlLeftClick;
}
private static void OnControlLeftClick(object sender, MouseButtonEventArgs e)
{
MyControl control = sender as MyControl;
if (control == null || control.Command == null) return;
ICommand command = control.Command;
if (command.CanExecute(null))
command.Execute(null);
}
}
XAML:
<local:MyControl Command="{Binding SomeCommand}"/>
Other controls
创建附加行为。
public static class ExecutesCommandOnLeftClickBehavior
{
public static ICommand GetCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(CommandProperty);
}
public static void SetCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(CommandProperty, value);
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
"Command",
typeof(ICommand),
typeof(ExecutesCommandOnLeftClickBehavior),
new PropertyMetadata(null, OnCommandPropertyChanged));
private static void OnCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement element = d as UIElement;
if (element == null) return;
element.MouseLeftButtonDown -= OnMouseLeftButtonDown;
element.MouseLeftButtonDown += OnMouseLeftButtonDown;
}
private static void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
UIElement element = sender as UIElement;
if (element == null) return;
ICommand command = GetCommand(element);
if (command == null) return;
if (command.CanExecute(null))
command.Execute(null);
}
}
XAML:
<Grid local:ExecutesCommandOnLeftClickBehavior.Command="{Binding SomeCommand}"/>
您可以将事件更改为MouseLeftButtonDown
以外的其他内容,如果需要,可以为CommandParameter添加DependencyProperty
(AttachedProperty
)。
另一答案
每个UIElement都有所谓的InputBindings
输入绑定支持将命令绑定到输入设备。例如,MouseBinding实现包含特定于鼠标设备的属性的输入绑定。
<Label Content="new">
<Label.InputBindings>
<MouseBinding Gesture="LeftClick"
Command="{Binding Path=myCommand}"
CommandParameter="smth"/>
</Label.InputBindings>
</Label>
因此,不是注册新的Command和CommandParameter DP并处理触发Command的事件,而是尝试使用开箱即用的功能
以上是关于将命令属性添加到任何自定义控件的主要内容,如果未能解决你的问题,请参考以下文章
添加列的自定义 WinForms DataGridView 问题
Google 跟踪代码管理器 - 将 CSP 随机数添加到自定义 HTML 代码段的任何可能方式?脚本属性被剥离