WPF命令参数CommandParameter
2024-09-01 07:38:38
XAML代码如下:
<Window x:Class="Demo006.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="240" Width="360" WindowStyle="ToolWindow">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="24" />
<RowDefinition Height="4" />
<RowDefinition Height="24" />
<RowDefinition Height="4" />
<RowDefinition Height="24" />
<RowDefinition Height="4" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Name:" VerticalAlignment="Center" HorizontalAlignment="Left" />
<TextBox Grid.Row="0" Name="NameTextBox" Margin="60,0,0,0" />
<Button Grid.Row="2" Content="New Teacher" Command="New" CommandParameter="Teacher" />
<Button Grid.Row="4" Content="New Student" Command="New" CommandParameter="Student" />
<ListBox Grid.Row="6" Name="NewItemListBox" Height="117" VerticalAlignment="Bottom" />
</Grid>
<Window.CommandBindings>
<CommandBinding Command="New"
CanExecute="CommandBinding_CanExecute"
Executed="CommandBinding_Executed" />
</Window.CommandBindings>
</Window>
相关的事件处理器代码如下所示:
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (string.IsNullOrEmpty(this.NameTextBox.Text) == true) e.CanExecute = false;
else e.CanExecute = true;
} private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
string name = this.NameTextBox.Text;
if (e.Parameter.ToString() == "Teacher")
{
this.NewItemListBox.Items.Add(string.Format("New Teacher: {0}, 学而不厌,诲人不倦。", name));
}
else if (e.Parameter.ToString() == "Student")
{
this.NewItemListBox.Items.Add(string.Format("New Student: {0},好好学习,天天向上。", name));
}
}
最新文章
- 多本地代码工作点更新到2个远端GIT仓库
- 使用do{ } while(0)的好处
- [原]通过配合ffmpeg.exe获取视频文件时长
- linux指令之文件的创建、查询、修改
- java中instanceof和getClass()的区别分析
- barrier()函数
- Error in notifier
- 13、主线程任务太多导致异常退出(The application may be doing too much work on its main thread)
- 三分--Football Goal(面积最大)
- js实现全屏
- ThinkPHP中的动态缓存(S方法)和快速缓存(F方法)(转)
- 在ADS上由于volatile惹得祸
- UWP 实现App多语言为所欲为切换
- Windows下基于ADS+J-Link 的ARM开发环境搭建
- vue element-ui 分页组件封装
- Socket通信例子
- IDEA常用快捷键总结
- 07-查询操作(DQL)-多表查询
- 【python】查找函数定义
- create database link
热门文章
- PHP实现的一分页工具类代码
- 【FitNess】测试框架试用
- API - .add()
- html中调用silverlight中的方法
- wpf的datepicker处理
- 与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。
- 011--VS2013 C++ 斜角地图贴图
- 编译QT时出现lib/libQtGui.so: undefined reference to `ts_read_raw&#39;的解决办法
- php对象
- plist 读取 swift