C# WPF:如何实现Clear命令
我正在学习WPF 在其中一个练习中,我有一个文本框和剪切粘贴的按钮。以下内容足以实现剪切和粘贴功能: XAML: 唉,这行不通。应用程序命令没有明确的名称。我是否应该实现中建议的自定义命令 我尝试了以下方法: 我在我的窗口中实现了CanExecute和Executed方法:C# WPF:如何实现Clear命令,c#,wpf,C#,Wpf,我正在学习WPF 在其中一个练习中,我有一个文本框和剪切粘贴的按钮。以下内容足以实现剪切和粘贴功能: XAML: 唉,这行不通。应用程序命令没有明确的名称。我是否应该实现中建议的自定义命令 我尝试了以下方法: 我在我的窗口中实现了CanExecute和Executed方法: public partial class CustomCommandSample : Window { public CustomCommandSample() { InitializeCom
public partial class CustomCommandSample : Window
{
public CustomCommandSample()
{
InitializeComponent();
}
private void ClearCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void ClearCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
txtEditor.Clear();
}
}
静态自定义命令类:
public static class CustomCommands
{
public static RoutedUICommand Clear => new RoutedUICommand (
"Clear",
"Clear",
typeof(CustomCommands));
}
最后,XAML:
注意:此项目中的类位于命名空间WpfCommandDemo中。Xaml将其称为本地
我应该使用自定义命令解决这个问题吗?我应该换什么?或者我完全错了,如果我以不同的方式解决这个问题要在XAML中使用CustomCommands,您需要添加对它的引用。在元素中,添加一行:
xmlns:custom="clr-namespace:MyApplication.NamespaceWithCustomInIt"
根据需要替换命名空间值。然后您应该能够在XAML中的任何地方引用CustomCommands作为custom:CustomCommands可能必须绑定,我稍后会检查
我应该使用自定义命令解决这个问题吗
对。这就是如何使用Model-View-ViewModel-MVVM设计模式来解决这个问题,这是开发基于XAML的UI应用程序时推荐使用的设计模式
从博客帖子:
WPF提供了两种ICommand接口的实现;System.Windows.Input.RoutedCommand和System.Windows.Input.RoutedCommand,后者是前者的子类,后者只是添加一个描述命令的文本属性。但是,这两种实现都不特别适合在视图模型中使用,因为它们从关注的元素开始搜索可视化树,并向上搜索在CommandBindings集合中具有匹配System.Windows.Input.CommandBinding对象的元素,然后执行此特定CommandBinding的执行委托。由于命令逻辑应该驻留在视图模型中,因此您不希望为了将命令连接到可视元素而在视图中设置CommandBinding。相反,您可以通过创建实现ICommand的类来创建自己的命令。以下实现是调用Execute和CanExecute方法的委托的常见实现:
在视图中,只需绑定到视图模型的属性:
<TextBox AcceptsReturn="True" Name="txtEditor" Text="{Binding Text}" />
<Button Content="Clear" Command="{Binding ClearCommand}" />
你可以实现你自己的中继命令,看看这个
<Window x:Class="WpfTutorialSamples.Commands.UsingCommandsSample"
xmlns="...
xmlns:local="clr-namespace:WpfCommandDemo"
Title="UsingCommandsSample" Height="100" Width="200">
<Window.CommandBindings>
<CommandBinding Command="CustomCommands.Clear"
CanExecute="ClearCommand_CanExecute"
Executed="ClearCommand_Executed" />
</Window.CommandBindings>
<DockPanel>
<WrapPanel DockPanel.Dock="Top" Margin="3">
<Button Command="CustomCommands.Clear"
CommandTarget="{Binding ElementName=txtEditor}"
Width="60">
Clear
</Button>
... (other buttons: cut / paste, as above
</WrapPanel>
<TextBox AcceptsReturn="True" Name="txtEditor" />
</DockPanel>
Type reference cannot find type named
'{http://schemas.microsoft.com/winfx/2006/xaml/presentation}CustomCommands'.
xmlns:custom="clr-namespace:MyApplication.NamespaceWithCustomInIt"
public class DelegateCommand: System.Windows.Input.ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public DelegateCommand(Action<object> execute)
: this(execute, null) { }
public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter) => _canExecute == null ? true : _canExecute(parameter);
public void Execute(object parameter) => _execute(parameter);
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
ClearCommand = new DelegateCommand(Clear);
}
private string _text;
public string Text
{
get { return _text; }
set { _text = value; NotifyPropertyChanged(); }
}
public ICommand ClearCommand { get; }
private void Clear(object parameter)
{
Text = string.Empty;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
<TextBox AcceptsReturn="True" Name="txtEditor" Text="{Binding Text}" />
<Button Content="Clear" Command="{Binding ClearCommand}" />
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}