xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:MyRouteCommand"
x:Class="MyRouteCommand.Window1"
Title="RouteCommand" Height="300" Width="300"
xmlns:d="http://schemas.microsoft.com/expression/blend/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
>
<Window.CommandBindings>
<CommandBinding Command="{x:Static custom:Window1.MyCmd}"
Executed="CmdExecuted"
CanExecute="CmdCanExecute"/>
</Window.CommandBindings>
<Grid>
<Button x:Name="Mybtn1" Height="30" Width="30" Click="Btn_Click" d:LayoutOverrides="Width" HorizontalAlignment="Left" Margin="67,116,0,120" Content="B1">
<!--<Button.CommandBindings>
<CommandBinding Command="{x:Static custom:Window1.MyCmd}"
Executed="CmdExecuted"
CanExecute="CmdCanExecute"/>
</Button.CommandBindings>-->
</Button>
<Button x:Name="Mybtn2" Margin="0,116,91,120" Content="B2" Width="34" HorizontalAlignment="Right" Click="Btn2_Click"/>
</Grid>
</Window>
CS
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MyRouteCommand
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : System.Windows.Window
{
public static RoutedCommand MyCmd = new RoutedCommand();
public Window1()
{
InitializeComponent();
}
private void CmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Command has been excuted, the source is" + e.Source.ToString());
}
// CanExecuteRoutedEventHandler for the ColorCmd
private void CmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (e.Source != null)
{
e.CanExecute = true;
}
else
{
e.CanExecute = false;
}
}
private void Btn_Click(object sender, EventArgs e)
{
MyCmd.Execute(null, Mybtn1);
}
private void Btn2_Click(object sender, RoutedEventArgs e)
{
MyCmd.Execute(null, Mybtn2);
}
}
}
本文介绍了一个使用WPF路由命令的示例程序。该程序定义了一个静态的路由命令,并通过按钮触发命令执行。当命令被触发时,会显示消息框通知用户命令已被执行。
1573

被折叠的 条评论
为什么被折叠?



