-
- 命令是任何一个实现了ICommand接口(位于System.Windows.Input命名空间)的对象,每个对象定义了3个简单的成员:
- Execute:执行特定命令的逻辑的方法。
- CanExecute:如果命令允许被执行,则该方法返回true;如果不允许执行,则返回false。
- CanExecuteChanged——无论何时,只要CanExecute的值改变,该事件就会触发。
- 创建自定义命令例子
- WPF有5个类的静态属性实现了WPF的内建命令:
- ApplicationCommands:
- ComponentCommands:
- MediaCommands:
- NavigationCommands:
- EditingCommands:
- 内建命令使用的流程
- 将元素的Command特性设置为内建命令
- 向元素或父元素添加CommandBinding对象。
- 内建命令的Text字符串是由每个RoutedUICommand定义的,WPF会自动对Text作本地化,并把它转换为任何一种WPF支持的语言。
- 内建命令使用示例
- 命令是任何一个实现了ICommand接口(位于System.Windows.Input命名空间)的对象,每个对象定义了3个简单的成员:
3.4.1 内建命令
XAML文件
<Window x:Class="Custom_RoutedCommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:Custom_RoutedCommand"
Title="Custom RoutedCommand Sample"
Name="RootWindow"
Height="500"
Width="600"
Focusable="True">
<Window.CommandBindings>
<CommandBinding Command="{x:Static custom:MainWindow.ColorCmd}"
Executed="ColorCmdExecuted"
CanExecute="ColorCmdCanExecute" />
</Window.CommandBindings>
<DockPanel>
<Menu DockPanel.Dock="Top"
Height="25">
<MenuItem Header="Commands">
<MenuItem Header="Color Command"
Command="{x:Static custom:MainWindow.ColorCmd}" />
</MenuItem>
</Menu>
<Border BorderBrush="Black"
BorderThickness="1"
Margin="10"
Height="175"
Width="375"
DockPanel.Dock="Top">
<TextBlock TextWrapping="Wrap"
Margin="3">
The Color Command chages the background color of a StackPanel.
<LineBreak />
<LineBreak />
In order for the command attached to the MenuItem to be enabled, the
StackPanel must have keyboard focus. Use the TAB key to change focus.
<LineBreak />
<LineBreak />
Since the command attached to the Button has the CommandTarget
set to the first StackPanel, the StackPanel does not need focus.
<LineBreak />
</TextBlock>
</Border>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"
DockPanel.Dock="Bottom">
<Border BorderBrush="Black"
BorderThickness="1"
Margin="10"
Height="200"
Width="200">
<StackPanel Name="FirstStackPanel"
Background="AliceBlue"
Focusable="True">
<StackPanel.CommandBindings>
<CommandBinding Command="{x:Static custom:MainWindow.ColorCmd}"
Executed="ColorCmdExecuted"
CanExecute="ColorCmdCanExecute" />
</StackPanel.CommandBindings>
<Label>First StackPanel</Label>
<Button Command="{x:Static custom:MainWindow.ColorCmd}"
CommandParameter="ButtonOne"
CommandTarget="{Binding ElementName=FirstStackPanel}"
Content="CommandTarget = FirstStackPanel" />
</StackPanel>
</Border>
<Border BorderBrush="Black"
BorderThickness="1"
Margin="10"
Height="200"
Width="200">
<StackPanel Background="AliceBlue"
Focusable="True">
<Label>Second StackPanel</Label>
</StackPanel>
</Border>
</StackPanel>
</DockPanel>
</Window>
对应的代码隐藏文件
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Custom_RoutedCommand
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static RoutedCommand ColorCmd = new RoutedCommand();
public MainWindow()
{
InitializeComponent();
}
// ExecutedRoutedEventHandler for the custom color command.
private void ColorCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
Panel target = e.Source as Panel;
if (target != null)
{
if (target.Background == Brushes.AliceBlue)
{
target.Background = Brushes.LemonChiffon;
}
else
{
target.Background = Brushes.AliceBlue;
}
}
}
// CanExecuteRoutedEventHandler for the custom color command.
private void ColorCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (e.Source is Panel)
{
e.CanExecute = true;
}
else
{
e.CanExecute = false;
}
}
}
}
Close、Copy、Cut、Delete、Find、Help、New、Open、Paste、Print、PrintPreview、Properties、Redo、Replace、Save、SaveAs、SelectAll、Stop、Undo
MoveDown、MoveLeft、MoveRight、MoveUp、ScrollByLine、ScrollPageDown、ScrollPageLeft、ScrollPageRight、ScrollPageUp、SelectToEnd、SelectToHome、SelectToPageDown、SelectToPageUp
ChannelDown、ChannelUp、DecreaseVolume、FastForward、IncreaseVolume、MuteVolume、NextTrack、Pause、Play、PreviousTrack、Record、Rewind、Select、Stop
BrowseBack、BrowseForward、BrowseHome、BrowseStop、Favorites、FirstPage、GoToPage、LastPage、NextPage、PreviousPage、Refresh、Search、Zoom
AlignCenter、AlignJustify、AlignLeft、AlignRight、CorrectSpellingError、DecreaseFontSize、DecreaseIndentation、EnterLineBreak、EnterParagraphBreak、IgnoreSpellingError、IncreaseFontSize、IncreaseIndentation、MoveDownByLine、MoveDownByPage、MoveDownByParagraph、MoveLeftByCharacter、MoveLeftByWord、MoveRightByCharacter、MoveRightByWord
每个属性并不会返回实现ICommand的独特类型,相反,它们都是RoutedUICommand的实例。RoutedUICommand类不仅实现了ICommand接口,还可以像路由事件一样支持冒泡。
例如:
helpButton.Command = ApplicationCommands.Help;
例如:
this.CommandBings.Add(new CommandBinding(ApplicationCommands.Help, this.HelpExecuted, this.HelpCanExecute));
其中:ApplicationCommands.Help 是内建命令
this.HelpExecuted 是自定义执行命令的内容方法
this.HelpCanExecute 是自定义命令是否可执行的方法
XAML文件
<Window x:Class="WpfApplication1.AboutDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="About WPF Unleashed"
SizeToContent="WidthAndHeight"
Background="OrangeRed">
<Window.CommandBindings>
<CommandBinding Command="Help"
CanExecute="HelpCanExecute"
Executed="HelpExecuted" />
</Window.CommandBindings>
<StackPanel>
<Label FontWeight="Bold"
FontSize="20"
Foreground="White">
WPF Unleashed (Version 3.0)
</Label>
<Label>
© 2006 SAMS Publishing
</Label>
<Label>
Installed Chapters:
</Label>
<ListBox>
<ListBoxItem>
Chapter 1
</ListBoxItem>
<ListBoxItem>
Chapter 2
</ListBoxItem>
</ListBox>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center">
<Button MinWidth="75"
Margin="10"
Command="Help"
Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}" />
<Button MinWidth="75"
Margin="10">
OK
</Button>
</StackPanel>
<StatusBar>
You have successfully registered this product.
</StatusBar>
</StackPanel>
</Window>
对应的代码隐藏文件
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class AboutDialog : Window
{
public AboutDialog()
{
InitializeComponent();
}
private void HelpCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.adamnathan.net/wpf");
}
}
}