RouteCommand Sample

本文介绍了一个使用WPF路由命令的示例程序。该程序定义了一个静态的路由命令,并通过按钮触发命令执行。当命令被触发时,会显示消息框通知用户命令已被执行。
部署运行你感兴趣的模型镜像

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);
        }

    }
}
 

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值