WPF CommandBinding CommandParameter 用法

1.XAML部分


    


    
        
            
            
            
            
            
            
            
            
        

        
        

        
        
        
        


 

2.CS部分

        private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //用this.NameTextBox.Text来调出本界面下的控件元素属性部分
            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));
            }
        }

你可能感兴趣的:(WPF,wpf)