(5)WPF深入解析命令

1、WPF命令

1)命令和事件的区别

  • 命令可以Bingding,可以在通过地方复用命令。而事件不能绑定,复用受到限制(在不同后台文件中,不能复用)
  • 命令还有CanExecute和CanExecuteChanged方法。

2)示例

命令示例:

<Button  Name="ResetCenterLine" AutomationProperties.AutomationId="ID_BTN_APA_RESETCENTERLINE"
        Width="35" Height="35" Margin="10,0,0,0" Focusable="False"
        Command="{Binding CenterlineUndoRedoCommand}" CommandParameter="{x:Static local:CenterlineOperationType.ReSetCenterline}"
        Style="{DynamicResource Style_Button_Common_CSW_Default}"
        ToolTip="{DynamicResource UID_APA_Reset}" HorizontalAlignment="Center" VerticalAlignment="Center">
</Button>

后台代码

private RelayCommand<CenterlineOperationType> _centerlineUndoRedoCommand;
public RelayCommand<CenterlineOperationType> CenterlineUndoRedoCommand
{
    get
    {
        return _centerlineUndoRedoCommand ??(_centerlineUndoRedoCommand = new RelayCommand<CenterlineOperationType>(
            (x) =>
            {
                Container.GetCommunicationModule().AsyncSendCommand(ByteString.CopyFromUtf8(x.ToString()), SendToBEOperationName.UndoRedoCenterline);

            }));
    }
}

 

posted @ 2020-08-10 19:45  代码吸血虫  阅读(143)  评论(0)    收藏  举报