将命令添加到按钮绑定 WPF 的自定义控件
本文关键字:WPF 自定义控件 绑定 按钮 命令 添加 | 更新日期: 2023-09-27 18:33:10
我想向我的自定义控件添加一个方法,我可以在 MainWindow.xaml 中使用命令绑定从按钮调用该方法。我在网上遇到了一些解决方案,但是其中一个似乎不起作用,而另一个则有效。有人可以向我解释设置它的正确方法吗?第一个解决方案产生和错误,如下所述。第二个解决方案有效,但我不确定任何优缺点。
解决方案 1 - 损坏
public partial class MyControl : Control
{
...
public static readonly RoutedCommand AlignLeftCommand = null;
static MyControl()
{
binding = new CommandBinding();
binding.Command = AlignLeftCommand;
binding.Executed += new ExecutedRoutedEventHandler(AlignLeft_Executed);
CommandManager.RegisterClassCommandBinding(typeof(MyControl), binding);
}
}
错误:
严重性代码描述项目文件行 错误 CS0120 非静态字段、方法或属性需要对象引用...
解决方案 2
public partial class MyControl : Control
{
...
public static readonly RoutedCommand AlignLeftCommand = new RoutedCommand();
public MyControl()
{
this.CommandBindings.Add(new CommandBinding(MyControl.AlignLeftCommand, AlignLeft_Executed, null));
}
}
这是调用该方法的按钮。
<StackPanel Orientation="Horizontal">
<Button Content="Left Edges" FontSize="8"
Command="{x:Static JM:MyControl.AlignLeftCommand}"
CommandTarget="{Binding ElementName=mycontrol}"/>
</StackPanel>
首先,您应该像这样在Window
上定义一个命令绑定(为 Executed
和 CanExecute
事件创建处理程序):
<Window x:Class="CommandBindingWPF.MainWindow"
...The code omitted for the brevity...
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute" />
</Window.CommandBindings>
并声明您的Button
ix xaml :
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Command="ApplicationCommands.New">New</Button>
</StackPanel>
创建命令绑定后,应在代码隐藏中创建处理程序:
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Hello from Command");
}
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{ }
更新:
对于 MVVM 应用程序:
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion // ICommand Members
}
然后在视图模型中创建一个属性。例如:
public class YourViewModel
{
public RelayCommand YourCommand { get; set; }
public YourViewModel()
{
YourCommand = new RelayCommand(DoSmth, CanDoSmth);
}
private void DoSmth(object obj)
{
Message.Box("Hello from viewModel");
}
private bool CanDoSmth(object obj)
{
//you could implement your logic here. But by default it should be
//set to true
return true;
}
}
XAML 应如下所示:
<Button Content="Click me!" Command="{Binding YourCommand}"/>
要熟悉MVVM,我建议您阅读Rachel Lim的博客。她有教人的天赋,她可以用简单的术语来解释。阅读Rachel Lim的博客。要熟悉 MVVM 命令,请参阅该帖子