<Window x:Class="WpfApp8.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:WpfApp8"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.CommandBindings>
<CommandBinding Command="New" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed"></CommandBinding>
</Window.CommandBindings>
<Grid>
<StackPanel>
<TextBox x:Name="txt" Margin="5" ></TextBox>
<Button x:Name="NewTeacher" Margin="5" Content="NewTeacher" Command="New" CommandParameter="Teacher"></Button>
<Button x:Name="NewStudent" Margin="5" Content="NewStudent" Command="New" CommandParameter="Student"></Button>
</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.Navigation;
using System.Windows.Shapes;
namespace WpfApp8
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (string.IsNullOrEmpty(txt.Text))
{
e.CanExecute = false;
}
else
{
e.CanExecute = true;
}
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
string name = txt.Text;
MessageBox.Show(e.Parameter.ToString());
}
}
}