当前位置:   article > 正文

wpf 命令参数(CommandParameter)的使用_wpf commandparameter

wpf commandparameter
  1. <Window x:Class="WpfApp03.TestWin07"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:WpfApp03"
  7. mc:Ignorable="d"
  8. Title="TestWin07" Height="450" Width="800">
  9. <Grid Margin="6">
  10. <Grid.RowDefinitions>
  11. <RowDefinition Height="24"></RowDefinition>
  12. <RowDefinition Height="4"></RowDefinition>
  13. <RowDefinition Height="24"></RowDefinition>
  14. <RowDefinition Height="4"></RowDefinition>
  15. <RowDefinition Height="24"></RowDefinition>
  16. <RowDefinition Height="4"></RowDefinition>
  17. <RowDefinition Height="*"></RowDefinition>
  18. </Grid.RowDefinitions>
  19. <TextBlock Text="Name:" VerticalAlignment="Center" HorizontalAlignment="Left"/>
  20. <TextBox x:Name="txtName" Margin="60,0,0,0"/>
  21. <Button Content="New Teacher" Command="New" CommandParameter="Teacher" Grid.Row="2"/>
  22. <Button Content="New Student" Command="New" CommandParameter="Student" Grid.Row="4"/>
  23. <ListBox x:Name="lstNewItems" Grid.Row="6"/>
  24. </Grid>
  25. <Window.CommandBindings>
  26. <CommandBinding x:Name="New" Command="New" CanExecute="New_CanExecute" Executed="New_Executed"/>
  27. </Window.CommandBindings>
  28. </Window>
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. namespace WpfApp03
  15. {
  16. /// <summary>
  17. /// TestWin07.xaml 的交互逻辑
  18. /// </summary>
  19. public partial class TestWin07 : Window
  20. {
  21. public TestWin07()
  22. {
  23. InitializeComponent();
  24. }
  25. private void New_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  26. {
  27. if (string.IsNullOrEmpty(txtName.Text))
  28. {
  29. e.CanExecute = false;
  30. }
  31. else
  32. {
  33. e.CanExecute = true;
  34. }
  35. }
  36. private void New_Executed(object sender, ExecutedRoutedEventArgs e)
  37. {
  38. string name = txtName.Text;
  39. if (e.Parameter.ToString()=="Teacher")
  40. {
  41. lstNewItems.Items.Add(string.Format("New Teacher:{0},学而不厌,诲人不倦", name));
  42. }
  43. else if (e.Parameter.ToString()=="Student")
  44. {
  45. lstNewItems.Items.Add(string.Format("New Student:{0},好好学习,天天向上", name));
  46. }
  47. }
  48. }
  49. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/613361
推荐阅读
相关标签
  

闽ICP备14008679号