WPF Command

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF Command相关的知识,希望对你有一定的参考价值。

使用CustomControl时绑定Command用法

C# Part

 1 public static RoutedUICommand ClearCommand { get; private set; }
 2 
 3 static CustomControl()
 4 {
 5     DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
 6     CustomControl.ClearCommand = new RoutedUICommand();
 7     CommandBinding ClearCommandBinding=new CommandBinding(CustomControl.ClearCommand,CustomControl.ClearText);
 8     CommandManager.RegisterClassCommandBinding(typeof(CustomControl), ClearCommandBinding);
 9 }
10 
11 private static void ClearText(object sender, ExecutedRoutedEventArgs e)
12 {
13     ...
14 }

XAML Part

1 <Style TargetType="local:CustomControl">
2     <Setter Property="Template">
3         <Setter.Value>
4             <ControlTemplate TargetType="local:LabelEditCtrl">
5                 <Button Command="{x:Static local:CustomControl.ClearCommand}"/>
6             </ControlTemplate>
7         </Setter.Value>
8     </Setter>
9 </Style>

 

以上是关于WPF Command的主要内容,如果未能解决你的问题,请参考以下文章

WPF 定义Command

WPF自学入门WPF MVVM模式Command命令

WPF 事件实现MVVM中的Command绑定

WPF--鼠标右键菜单中的Command命令实现

Zara带你快速入门WPF---Command与功能区控件

七个办法只有一个有效:200 PORT command successful. Consider using PASV.425 Failed to establish connection.(代码片段