程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> WPF(命令參數)

WPF(命令參數)

編輯:C#入門知識

[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:" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.Row="0" /> 
        <TextBox x:Name="newTextBox" Margin="60,0,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:" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.Row="0" />
        <TextBox x:Name="newTextBox" Margin="60,0,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},好好學習,天天向上。", 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},好好學習,天天向上。", name));
            }
        }
    }
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved