WPF自定义快捷键命令Command的应用

文章参考https://blog.csdn.net/u011854789/article/details/57084872
https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/advanced/commanding-overview
https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/advanced/xaml-namespaces-and-namespace-mapping-for-wpf-xaml?view=netframework-4.7.2
WPF 中的命令是通过实现 ICommand 接口创建的。ICommand 公开两个方法(Execute 及 CanExecute)和一个事件(CanExecuteChanged)。Execute 执行与命令关联的操作。CanExecute 确定是否可以在当前命令目标上执行命令。如果集中管理命令操作的命令管理器检测到命令源中发生了更改,此更改可能使得已引发但尚未由命令绑定执行的命令无效,则将引发 CanExecuteChanged。ICommand 的 WPF 实现是 RoutedCommand 类。
还可以使用RoutedUICommand 类创建用户自定义命令。通过一个实例,新建一个EPF项目,在其中加入一个TextBlock,Button1和Button2来改变字体大小。

在这里插入图片描述

    <Grid>
        <TextBlock x:Name="textBlock1" HorizontalAlignment="Center" Margin="107,84,0,0" TextWrapping="Wrap" Text="hello World" VerticalAlignment="Top" Height="175" Width="307" FontSize="10"/>
        <Button x:Name="Button1" Content="Button1" HorizontalAlignment="Left" Margin="85,276,0,0" VerticalAlignment="Top" Width="76" Command="{StaticResource IncreaseFontSize}"/>
        <Button x:Name="Button2" Content="Button2" HorizontalAlignment="Left" Margin="265,276,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.5,-0.148" Command="{StaticResource DecreaseFontSize}"/>
    </Grid>

首先在Window.Resource中定义两个RoutedUICommand,分别用于增加和减小字体尺寸。
注意: Text=“Increase Font Size” 和Text=“Decrease Font Size” 必须要有。否则会出现:值不能为 null,参数名: value 。

    <Window.Resources>
        <RoutedUICommand x:Key="IncreaseFontSize" Text="Increase Font Size" />
        <RoutedUICommand x:Key="DecreaseFontSize" Text="Decrease Font Size" />
    </Window.Resources>

通过KeyBinding 为上面两个命令绑定快捷键,按键组合可使用“+”进行连接。MSDN 中建议使用Gesture 方式定义以免发生混淆。

    <Window.InputBindings>
        <KeyBinding Gesture="Ctrl+P" Command="{StaticResource IncreaseFontSize}"/>
        <KeyBinding Gesture="Ctrl+O" Command="{StaticResource DecreaseFontSize}"/>
    </Window.InputBindings>

接下来就要通过CanExecute和Excuted 为命令绑定相关的事件,CanExecute 负责判断能否执行命令(即Executed 定义的事件),Executed 就负责去执行用户定义的操作命令。

<Window.CommandBindings>
    <CommandBinding Command="{StaticResource IncreaseFontSize}"
                CanExecute="CommandBinding_Increase_CanExecute"
                Executed="CommandBinding_Increase_Executed"/>
    <CommandBinding Command="{StaticResource DecreaseFontSize}"
                CanExecute="CommandBinding_Decrease_CanExecute"
                Executed="CommandBinding_Decrease_Executed"/>
</Window.CommandBindings>
 private void CommandBinding_Increase_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //如果 CanExecute 返回 false 则禁用,如果 CanExecute 返回 true 则启用。
            if (textBlock1.FontSize > 50)
            {
                e.CanExecute = false;
            }
            else
            {
                e.CanExecute = true;
            }
        }

        private void CommandBinding_Increase_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            textBlock1.FontSize += 5;
        }

        private void CommandBinding_Decrease_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (textBlock1.FontSize <= 5)
            {
                e.CanExecute = false;
            }
            else
            {
                e.CanExecute = true;
            }
        }

        private void CommandBinding_Decrease_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            textBlock1.FontSize -= 5;
        }

进入到C# 中编写命令事件相关内容。扩大字体尺寸时通过CommandBinding_Increase_CanExecute 判断当前字体是否小于50,否则不会执行Executed 命令。若字体尺寸在50以内则通过CommandBinding_Increase_Executed 每次增加5。缩小尺寸时则不低于5。
A按下快捷键Ctrl+O或Button1,字体会放大。
在这里插入图片描述
A按下快捷键Ctrl+P或Button2,字体会缩小。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值