<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. css3-8 内外边距中的注意要点有哪些

    css3-8 内外边距中的注意要点有哪些 一.总结 一句话总结:padding,border都是外延的.margin会合并. 1.两元素样式都有margin:15px,他们中间的距离是15px还是30 ...

  2. HTTPS和SSL/TLS协议

    要说清楚 HTTPS 协议的实现原理,至少需要如下几个背景知识.1. 大致了解几个基本术语(HTTPS.SSL.TLS)的含义2. 大致了解 HTTP 和 TCP 的关系(尤其是“短连接”VS“长连接 ...

  3. C - The C Answer (2nd Edition) - Exercise 1-6

    /* Verify that the expression getchar() != EOF is 0 or 1. */ #include <stdio.h> main() { int c ...

  4. JNDI 的理解

    JNDI是 Java 命名与文件夹接口(Java Naming and Directory Interface),在J2EE规范中是重要的规范之中的一个,不少专家觉得,没有透彻理解JNDI的意义和作用 ...

  5. 【hdu 1067】Gap

    Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission( ...

  6. 第六章:任务执行——Java并发编程实战

    任务:通常是一些抽象的且离散的工作单元.大多数并发应用程序都是围绕"任务执行"来构造的,把程序的工作分给多个任务,可以简化程序的组织结构便于维护 一.在线程中执行任务 任务的独立性 ...

  7. Qt for Automation

    Automation, Automotive, and other industries In addition to improving the generic product offering a ...

  8. Change Sudoers Mod 777 To 0440

    Method 1: > grub --> recovery mode --> e > ro single <--> rw single init=/bin/bash ...

  9. 如何在使Xcode打包iOS应用时自动增加编译号

    在红框标注的输入框中输入:真机调试编译成功增加 echo $CONFIGURATION if [ "Release" == "${CONFIGURATION}" ...

  10. 【codeforces 779B】Weird Rounding

    [题目链接]:http://codeforces.com/contest/779/problem/B [题意] 问你要删掉几个数字才能让原来的数字能够被10^k整除; [题解] /* 数字的长度不大; ...