WPF (command parameters)

Source: Internet
Author: User

[Html]
<Window x: Class = "TestOfCommandParameter. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "350" Width = "525"
Background = "LightBlue" WindowStyle = "ToolWindow">
<Grid Margin = "6">
<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 Text = "Name:" verticalignment = "Center" HorizontalAlignment = "Left" Grid. Row = "0"/>
<TextBox x: Name = "newTextBox" Margin = "60, 0, 0"
Grid. Row = "0"/>
<Button Content = "New Teacher"
Command = "New"
CommandParameter = "Teacher"
Grid. Row = "2"/>
<Button Content = "New Student"
Command = "New"
CommandParameter = "Student"
Grid. Row = "4"/>
<ListBox x: Name = "listBoxNewItems"
Grid. Row = "6"/>
</Grid>

<Window. CommandBindings>
<CommandBinding Command = "New" CanExecute = "New_CanExecute"
Executed = "New_Executed"/>
</Window. CommandBindings>
</Window>

<Window x: Class = "TestOfCommandParameter. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "350" Width = "525"
Background = "LightBlue" WindowStyle = "ToolWindow">
<Grid Margin = "6">
<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 Text = "Name:" verticalignment = "Center" HorizontalAlignment = "Left" Grid. Row = "0"/>
<TextBox x: Name = "newTextBox" Margin = "60, 0, 0"
Grid. Row = "0"/>
<Button Content = "New Teacher"
Command = "New"
CommandParameter = "Teacher"
Grid. Row = "2"/>
<Button Content = "New Student"
Command = "New"
CommandParameter = "Student"
Grid. Row = "4"/>
<ListBox x: Name = "listBoxNewItems"
Grid. Row = "6"/>
</Grid>

<Window. CommandBindings>
<CommandBinding Command = "New" CanExecute = "New_CanExecute"
Executed = "New_Executed"/>
</Window. CommandBindings>
</Window>
[Csharp]
Using System. Windows;
 
Namespace TestOfCommandParameter
{
/// <Summary>
/// Interaction logic for MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent ();
}
 
Private void New_CanExecute (object sender, System. Windows. Input. CanExecuteRoutedEventArgs e)
{
If (string. IsNullOrEmpty (this. newTextBox. Text ))
{
E. CanExecute = false;
} Else
{
E. CanExecute = true;
}
}
 
Private void New_Executed (object sender, System. Windows. Input. ExecutedRoutedEventArgs e)
{
String name = this. newTextBox. Text;
If (e. Parameter. ToString () = "Teacher ")
{
This. listBoxNewItems. Items. Add (string. Format ("New Teacher: {0. ", Name ));
}
 
If (e. Parameter. ToString () = "Student ")
{
This. listBoxNewItems. Items. Add (string. Format ("New Student: {0}, good study, every day. ", Name ));
}
}
}
}

Using System. Windows;

Namespace TestOfCommandParameter
{
/// <Summary>
/// Interaction logic for MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent ();
}

Private void New_CanExecute (object sender, System. Windows. Input. CanExecuteRoutedEventArgs e)
{
If (string. IsNullOrEmpty (this. newTextBox. Text ))
{
E. CanExecute = false;
} Else
{
E. CanExecute = true;
}
}

Private void New_Executed (object sender, System. Windows. Input. ExecutedRoutedEventArgs e)
{
String name = this. newTextBox. Text;
If (e. Parameter. ToString () = "Teacher ")
{
This. listBoxNewItems. Items. Add (string. Format ("New Teacher: {0. ", Name ));
}

If (e. Parameter. ToString () = "Student ")
{
This. listBoxNewItems. Items. Add (string. Format ("New Student: {0}, good study, every day. ", Name ));
}
}
}
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.