美文网首页
WPF基本命令系统使用2

WPF基本命令系统使用2

作者: 玉米须须 | 来源:发表于2019-07-24 11:13 被阅读0次
<Window x:Class="CommandParematerDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CommandParematerDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="26"></RowDefinition>
                <RowDefinition Height="26"></RowDefinition>
                <RowDefinition Height="26"></RowDefinition>
                <RowDefinition Height="100"></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>

            <TextBlock Text="name:" HorizontalAlignment="Left"></TextBlock>
            <TextBox Name="nameTextBox" Margin="60,0,0,0"></TextBox>
            <Button Name="tButton" Content="new teacher" Grid.Row="1" Command="New" CommandParameter="Teacher"></Button>
            <Button Name="sButton" Content="new student" Grid.Row="2" Command="New" CommandParameter="Student"></Button>
            <ListView Name="aListView" Grid.Row="3"></ListView>
        </Grid>
    </Grid>
    <Window.CommandBindings>
        <CommandBinding Command="New" CanExecute="New_canExecute" Executed="New_execute"></CommandBinding>
    </Window.CommandBindings>
</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.Navigation;
using System.Windows.Shapes;

namespace CommandParematerDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

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


        void New_execute(Object sender, ExecutedRoutedEventArgs e) {
            string name = nameTextBox.Text;
            if (e.Parameter.ToString() == "Student")
            {
                aListView.Items.Add(string.Format($"New student:{name}"));
            }
            else {
                aListView.Items.Add(string.Format($"New Teacher:{name}"));
            }
        }
    }
}

界面上有两个按钮,一个用于新建teacher,一个用于新建student,都使用new命令,那么就是通过CommandParameter来区分,命令源一定是实现了ICommandResource接口的对象,而ICommandSource有一个属性就是CommandPrameter。

相关文章

  • WPF基本命令系统使用2

    界面上有两个按钮,一个用于新建teacher,一个用于新建student,都使用new命令,那么就是通过Comma...

  • WPF基本命令系统使用

  • Linux中使用命令管理目录和文件

    一、使用命令查看系统基本信息 1、uname命令查看内核版本、硬件平台 2、 hostname查看主机名 3、if...

  • CefSharp For WPF基本使用

    Nuget引用 直接搜索安装CefSharp.Wpf即可,因为CefSharp.Wpf依赖于其他三个包,所以会自动...

  • WPF DataGrid 基本使用

    效果如下: 1. 设计DataGrid展示的数据模型类 public class Song { public i...

  • 【WPF】Command 自定义命令

    在使用 MVVM 结构的 WPF 程序中,ViewModel 对 View 的响应是通过 Command (命令)...

  • 常用命令

    Linux基本应用-基本命令的使用 【课程目的】 1.了解命令行中使用Linux命令的特点 2.掌握文件目录类命令...

  • MongoDB--周国康笔记

    1. MongoDB命令帮助系统 2. 基本命令及实例 一基本命令 二基本DDL和DML 三启动与终止 四安全管理...

  • Day-2 噜噜

    Day-2 LINUX 基本命令 命令结构 command -option 参数 系统操作 free 查看内存...

  • linux学习笔记-day14-查看磁盘

    linux系统中使用df命令查看磁盘的容量、使用容量、剩余容量等 ,默认使用kb为单位显示。命令的输出基本上会存在...

网友评论

      本文标题:WPF基本命令系统使用2

      本文链接:https://www.haomeiwen.com/subject/rogtrctx.html