完善主體資料,免費贈送VIP會員!
      * 主體類型
      * 企業名稱
      * 信用代碼
      * 所在行業
      * 企業規模
      * 所在職位
      * 姓名
      * 所在行業
      * 學歷
      * 工作性質
      請先選擇行業
      您還可以選擇以下福利:
      行業福利,領完即止!

      下載app免費領取會員

      NULL

      ad.jpg

      二次開發教程:ICommand接口

      發布于:2019-07-25 15:12:08

      網友投稿

      更多

          // 摘要:

          //     Defines a command.

          public interface ICommand

          {

              //

              // 摘要:

              //     Occurs when changes occur that affect whether or not the command should execute.

              event EventHandler CanExecuteChanged;


              //

              // 摘要:

              //     Defines the method that determines whether the command can execute in its current

              //     state.

              //

              // 參數:

              //   parameter:

              //     Data used by the command. If the command does not require data to be passed,

              //     this object can be set to null.

              //

              // 返回結果:

              //     true if this command can be executed; otherwise, false.

              bool CanExecute(object parameter);

              //

              // 摘要:

              //     Defines the method to be called when the command is invoked.

              //

              // 參數:

              //   parameter:

              //     Data used by the command. If the command does not require data to be passed,

              //     this object can be set to null.

              void Execute(object parameter);


      上面這個是ICommand包含的類容,通過一個小例子來說明她的使用方法,


      首先在Window里添加一個Button和一個Textbox,給Button綁定一個命令,給Text box綁定一個int數字,當int 數字小于0是Button不可用。


      首先創建命令:


          public class OK_Command : ICommand

          {

              private ViewModel _vm = null;

              public OK_Command(ViewModel vm)

              {

                  _vm = vm;

              }

              public event EventHandler CanExecuteChanged;

              public void RaiseCanExecuteChanged()

              {

                  if(CanExecuteChanged!=null)

                  {

                      CanExecuteChanged(this, EventArgs.Empty);

                  }

              }

              public bool CanExecute(object parameter)

              {

                  if (_vm.Num < 0)

                      return false;

                  return true;

              }

              public void Execute(object parameter)

              {

                  MessageBox.Show("OK");

              }

          }


      創建View Model:


          public class ViewModel : INotifyPropertyChanged

          {

              private int _num = 0;

              public int Num

              {

                  get

                  {

                      return _num;

                  }

                  set

                  {

                      _num = value;

                      RaisePropertyChanged(nameof(Num));

                      RaisePropertyChanged((nameof(OK_Command)));

                  }

              }


              private OK_Command _okCommand = null;

              public OK_Command OK_Command

              {

                  get

                  {

                      if(_okCommand ==null)

                      {

                          _okCommand = new OK_Command(this);

                      }

                      _okCommand.RaiseCanExecuteChanged();

                      return _okCommand;

                  }

              }


              public event PropertyChangedEventHandler PropertyChanged;

              private void RaisePropertyChanged(string name)

              {

                  if(PropertyChanged!=null)

                  {

                      PropertyChanged(this, new PropertyChangedEventArgs(name));

                  }

              }

          }




      在Window里添加Button和TextBox 并設置綁定路徑


      <Window x:Class="ICommandDemo.MainWindow"

              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

              xmlns:local="clr-namespace:ICommandDemo"

              mc:Ignorable="d"

              Title="MainWindow" Height="350" Width="525">

          <Grid>

              <Button x:Name="button" Command="{Binding Path=OK_Command}" Content="Button" HorizontalAlignment="Left" Margin="334,268,0,0" VerticalAlignment="Top" Width="75"/>

              <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="288,176,0,0" TextWrapping="Wrap" Text="{Binding Path= Num,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>

          </Grid>

      </Window>




      最后在Window啟動設置Data Context


          public partial class MainWindow : Window

          {

              public MainWindow()

              {

                  InitializeComponent();

                  DataContext = new ViewModel();

              }

          }

      本文版權歸腿腿教學網及原創作者所有,未經授權,謝絕轉載。

      未標題-1.jpg

      上一篇:二次開發教程:C# 弱事件

      下一篇:二次開發教程:WPF 模板選擇器

      主站蜘蛛池模板: 国产一区二区三区在线| 久久精品一区二区| 国产色情一区二区三区在线播放| 国产亚洲情侣一区二区无码AV | 日韩在线一区二区三区免费视频| 国产精品视频第一区二区三区| 无码毛片一区二区三区中文字幕| 国产乱码精品一区二区三区中| 国产在线无码视频一区二区三区| 日本夜爽爽一区二区三区| 成人精品一区二区三区校园激情 | 亚洲中文字幕无码一区二区三区 | 国产成人一区在线不卡| 国内精品一区二区三区最新| 国产主播福利一区二区| 亚洲一区在线视频| 精品无人区一区二区三区在线 | 国产精品亚洲高清一区二区 | 国产av熟女一区二区三区| 国产激情一区二区三区在线观看 | 国产免费一区二区三区在线观看| 视频在线一区二区三区| 一区二区3区免费视频| 国产成人精品一区在线| 岛国无码av不卡一区二区| 精品伦精品一区二区三区视频| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 一区二区三区视频免费观看| 日本一区二区三区在线观看视频| 99偷拍视频精品一区二区| 亚洲AV网一区二区三区| 视频在线观看一区| 国产成人精品无人区一区| 久久国产精品免费一区| 精品少妇一区二区三区视频 | 日韩在线视频一区| 老鸭窝毛片一区二区三区| 久久亚洲中文字幕精品一区四| 果冻传媒董小宛一区二区| 亚洲区精品久久一区二区三区 | 久久一区二区明星换脸|