<Window x:Class="Wpf180706.Window5"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window5" Height="300" Width="300">
    <Window.CommandBindings>
        <CommandBinding Command="New" Executed="New_Execute" CanExecute="New_CanExecute"></CommandBinding>
    </Window.CommandBindings>
    <Grid>
        <StackPanel>
            <TextBox Name="txt"></TextBox>
            <Button Name="newTeacher" Command="New" CommandParameter="Teacher">NewTeacher</Button>
            <Button Name="newStudent"  Command="New" CommandParameter="Student">NewStudent</Button>
            <TextBlock Name="msg"></TextBlock>
        </StackPanel>
    </Grid>

</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 Wpf180706
{
    /// <summary>
    /// Interaction logic for Window5.xaml
    /// </summary>
    public partial class Window5 : Window
    {
        public Window5()
        {
            InitializeComponent();
        }

        private void New_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            msg.Text = e.Parameter.ToString();
        }

        private void New_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txt.Text))
            {
                e.CanExecute = false;
            }
            else
            {
                e.CanExecute = true;
            }
        }
    }
}

WPF CommandParameter的使用的更多相关文章

  1. 【WPF】CommandParameter解决多传参问题

    方法一:传参按钮控件自身绑定的ItemSource 用WAF框架实现MVVM,按钮的点击事件都要通过Command来传递到这个View对应的ViewModel上,再通过ViewModel传递到上层的C ...

  2. wpf mvvm模式下CommandParameter传递多参

    原文:wpf mvvm模式下CommandParameter传递多参 CommandParameter一般只允许设置一次,所以如果要传递多参数,就要稍微处理一下.我暂时还没找到更好的方案,下面介绍的这 ...

  3. WPF ContextMenu 在MVVM模式中绑定 Command及使用CommandParameter传参

    原文:WPF ContextMenu 在MVVM模式中绑定 Command及使用CommandParameter传参 ContextMenu无论定义在.cs或.xaml文件中,都不继承父级的DataC ...

  4. WPF命令参数CommandParameter

    XAML代码如下: <Window x:Class="Demo006.MainWindow" xmlns="http://schemas.microsoft.com ...

  5. WPF命令參数CommandParameter

    XAML代码例如以下: <Window x:Class="Demo006.MainWindow" xmlns="http://schemas.microsoft.c ...

  6. WPF中ContextMenu通过CommandParameter传参

    场景:ListBox中有个ContextMenu,希望点击其中一个菜单项的时候把ListBox当做CommandParameter传递给Command,但是发现无论是通过ElementName还是Re ...

  7. [WPF]解决模板中ContextMenu绑定CommandParameter的问题

    直接上代码,首先是一个ContextMenu的模板: <ContextMenu x:Key="Menu" BorderThickness="0.3" Fo ...

  8. WPF中DataGrid控件内Button的Command和CommandParameter的绑定

    场景:视频上传功能,上传列表使用DataGrid控件,视频有不同的状态对应不同的操作,DataGrid中最后一列为操作列,里面是Button控件.希望点击Button后执行对应的操作,但是设置Butt ...

  9. WPF 通过 CommandParameter 传递当前窗体到 ViewModel

    在应用 Command 模式中,需要在View上点击 一个按钮,需要将当前窗体作为参数传递为 command 两种方式传递当前窗体1.通过窗体名称(假设窗体名称为 ThisWindow)   < ...

随机推荐

  1. jquery-11 jquery中的事件切换如何实现

    jquery-11 jquery中的事件切换如何实现 一.总结 一句话总结:事件切换hover()和toggle()函数.参数两个,都是函数,依次执行两个函数. 1.如何实现单击切换图片? 用togg ...

  2. php面试题7(1、unset变量是删除栈变量,并不删除堆变量)(2、php爬虫特别简单: 可以file_get_contents和直接fopen)

    php面试题7(1.unset变量是删除栈变量,并不删除堆变量)(2.php爬虫特别简单: 可以file_get_contents和直接fopen) 一.总结 1.unset变量是删除栈变量,并不删除 ...

  3. html中的span有value属性么(可以作为自定义属性在jquery中用)

    html中的span有value属性么(可以作为自定义属性在jquery中用) 一.总结 可以作为自定义属性在jquery中用 二.html中的span有value属性么 value并不是span标签 ...

  4. 小强的HTML5移动开发之路(48)——(小练习)新闻订阅系统【1】

    一.总体设计 二.数据库设计 --新闻类别表 create table news_cate( news_cateid int primary key auto_increment, news_icon ...

  5. javax.servlet.WriteListener

    http://www.programcreek.com/java-api-examples/index.php?api=javax.servlet.WriteListener

  6. linux文件管理小结之自己定义more

    1.more命令功能 more命令用于查看内容超过一屏的文本(相似于cat) 基本功能: 1.输入backspace :内容翻一屏 2.输入enter : 内容翻一行 3.输入q:退出 4.实时显示已 ...

  7. [NPM] Add comments to your npm scripts

    The need for comments in your package.json file becomes desirable the more and more npm scripts you ...

  8. ARM汇编初探---汇编代码中都有哪几类指令---ARM伪指令介绍

    要学习一个东西首先要把概念搞清楚,以下仅仅是自己的一些关于汇编的理解. 可运行文件里的01码是机器码,机器码不等于汇编码,尽管机器码能够非常easy翻译成汇编码. 汇编码中包括非常多汇编指令.伪指令和 ...

  9. XMPP之ios即时通讯客户端开发-配置XMPP基本信息(四)

    前文已经有配置open fire,接下来要通过XMPP框架链接到open fire的服务器: 1.首先要在系统偏好设置里面打开open fire的服务器 2.代码中设置xmpp的myJID 有几个名词 ...

  10. Linux初接触设置笔记01

    没事装Linux尝试一下,来来回回装无数次,把刚开始需要设置的东西自己收藏一下,针对Centos7 装完Centos默认会覆盖windows引导,所以首先要做的是恢复windows的引导,如果不恢复, ...