• [织梦吧]唯一域名:www.dedecms8.com,织梦DedeCMS学习平台.

当前位置: > 编程与数据库 > net编程 >

WPF自定义快捷键命令(Command)(2)

来源: www.dedecms8.com 编辑:织梦吧 时间:2012-06-27点击:

<Window.InputBindings>
<KeyBinding Modifiers="Ctrl+Alt" Key="I" Command="{StaticResource IncreaseFontSize}"/>
<KeyBinding Gesture="Ctrl+Alt+D" 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>

至此,我们在XAML 中对命令的定义已经完成。下面进入到C# 中编写命令事件相关内容。扩大字体尺寸时通过CommandBinding_Increase_CanExecute 判断当前字体是否小于50,否则不会执行Executed 命令。若字体尺寸在50以内则通过CommandBinding_Increase_Executed 每次增加5。缩小尺寸时则不低于5。

private void CommandBinding_Increase_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
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;
}

 

首先在Window.Resources 中定义两个RoutedUICommand,分别用于增加和减小字体尺寸。

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

通过KeyBinding 为上面两个命令绑定快捷键,按键组合可使用“+”进行连接。下面代码分别通过Modifiers+Key 和Gesture 两种方式为定义快捷键组合方式。大家可以任选其一进行使用,MSDN 中建议使用Gesture 方式定义以免发生混淆。

<Window.InputBindings>
<KeyBinding Modifiers="Ctrl+Alt" Key="I" Command="{StaticResource IncreaseFontSize}"/>
<KeyBinding Gesture="Ctrl+Alt+D" 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>

至此,我们在XAML 中对命令的定义已经完成。下面进入到C# 中编写命令事件相关内容。扩大字体尺寸时通过CommandBinding_Increase_CanExecute 判断当前字体是否小于50,否则不会执行Executed 命令。若字体尺寸在50以内则通过CommandBinding_Increase_Executed 每次增加5。缩小尺寸时则不低于5。

private void CommandBinding_Increase_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
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;
}

 

首先在Window.Resources 中定义两个RoutedUICommand,分别用于增加和减小字体尺寸。

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

通过KeyBinding 为上面两个命令绑定快捷键,按键组合可使用“+”进行连接。下面代码分别通过Modifiers+Key 和Gesture 两种方式为定义快捷键组合方式。大家可以任选其一进行使用,MSDN 中建议使用Gesture 方式定义以免发生混淆。

<Window.InputBindings>
<KeyBinding Modifiers="Ctrl+Alt" Key="I" Command="{StaticResource IncreaseFontSize}"/>
<KeyBinding Gesture="Ctrl+Alt+D" 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>

至此,我们在XAML 中对命令的定义已经完成。下面进入到C# 中编写命令事件相关内容。扩大字体尺寸时通过CommandBinding_Increase_CanExecute 判断当前字体是否小于50,否则不会执行Executed 命令。若字体尺寸在50以内则通过CommandBinding_Increase_Executed 每次增加5。缩小尺寸时则不低于5。

private void CommandBinding_Increase_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
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;
}

 

About D8

  • ©2014 织梦吧(d8) DedeCMS学习交流平台
  • 唯一网址 www.DedeCMS8.com 网站地图
  • 联系我们 1170734538@qq.com ,  QQ