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>

 

posted on 2017-06-13 12:15  纯洁的兔子  阅读(1388)  评论(0编辑  收藏  举报

导航