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

      下載app免費領(lǐng)取會員

      NULL

      ad.jpg

      二次開發(fā)教程:WPF 給控件添加可以綁定的命令

      發(fā)布于:2019-07-25 15:07:29

      網(wǎng)友投稿

      更多

      在WPF里的Button有一個可以綁定的Command的屬性,只要綁定好這個屬性以后,只要你ClickButton就


      會運行這個命令,但這時我們可以考慮一下這個問題,為什么是Click來觸發(fā)呢?為什么不是右鍵單擊來觸發(fā)呢,


      下面研究一下,怎么能寫一個右鍵單機(jī)能觸發(fā)的命令:


      首先現(xiàn)有的Button肯定是不行了,所以自己寫一個TButton ,它繼承自Button


          public class TButton:Button

          {

              public static readonly DependencyProperty TCommandParameterProperty = DependencyProperty.Register("TCommandParameter", typeof(object), typeof(TButton));

              public static readonly DependencyProperty TCommandProperty = DependencyProperty.Register("TCommand", typeof(ICommand), typeof(TButton));

              public static readonly DependencyProperty TCommandTargetProperty = DependencyProperty.Register("TCommandTarget", typeof(object), typeof(TButton));

              public ICommand TCommand

              {

                  get

                  {

                      return (ICommand)GetValue(TCommandProperty);

                  }

                  set

                  {

                      SetValue(TCommandProperty, value);

                  }

              }

              public object TCommandParameter

              {

                  get

                  {

                      return GetValue(TCommandParameterProperty);

                  }

                  set

                  {

                      SetValue(TCommandParameterProperty, value);

                  }

              }

              public IInputElement TCommandTarget

              {

                  get

                  {

                      return (IInputElement)GetValue(TCommandTargetProperty);

                  }

                  set

                  {

                      SetValue(TCommandTargetProperty, value);

                  }

              }


              protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)

              {

                  base.OnMouseRightButtonUp(e);

                  RoutedCommand rcmd = TCommand as RoutedCommand;

                  if(rcmd!=null)

                  {

                      if(rcmd.CanExecute(TCommandParameter,TCommandTarget))

                      {

                          rcmd.Execute(TCommandParameter, TCommandTarget);

                      }                

                  }

                  else

                  {

                      if(TCommand!=null)

                      {

                          if(TCommand.CanExecute(TCommandParameter))

                          {

                              TCommand.Execute(TCommandParameter);

                          }

                      }

                  }

              }

          }


      再寫一個命令


          public class TCommand : ICommand

          {

              public event EventHandler CanExecuteChanged;

              public bool CanExecute(object parameter)

              {

                  return true;

              }

              public void Execute(object parameter)

              {

                  Window win = parameter as Window;

                  if (win != null)

                      win.Close();

              }

          }


      再界面里綁定:


              <local:TButton x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="411,277,0,0" VerticalAlignment="Top" Width="75" TCommand="{Binding TCommand}" TCommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>

      本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。

      未標(biāo)題-1.jpg

      上一篇:二次開發(fā)教程:WPF DataContent內(nèi)存釋放問題

      下一篇:二次開發(fā)教程:WPF 依賴屬性

      主站蜘蛛池模板: 无码午夜人妻一区二区不卡视频 | 久久久av波多野一区二区| 成人国产精品一区二区网站| 国产精品视频第一区二区三区| 色屁屁一区二区三区视频国产| 国产精品一区二区av不卡| 国产精品一区视频| 国产一区二区免费视频| 日本强伦姧人妻一区二区| 精品无码国产一区二区三区AV| 亚洲A∨精品一区二区三区| 无码中文字幕乱码一区| 无码人妻精品一区二区三区99仓本 | 老熟女高潮一区二区三区| 香蕉一区二区三区观| 精品人妻一区二区三区四区| 无码人妻精品一区二区三区久久久 | 久久一区二区三区免费播放| 韩国福利一区二区三区高清视频 | 人妻无码久久一区二区三区免费| 精品无码人妻一区二区三区不卡| 最新中文字幕一区二区乱码 | 久久99精品波多结衣一区| 久久毛片免费看一区二区三区| 杨幂AV污网站在线一区二区| 国产伦一区二区三区免费| 精品国产一区二区二三区在线观看| 亚洲AV一区二区三区四区| 日韩一区二区在线播放| 免费播放一区二区三区| 国产主播福利一区二区| 蜜臀AV无码一区二区三区| 亚洲乱色熟女一区二区三区蜜臀| 国内精品一区二区三区东京| 一区二区三区视频| 国模无码一区二区三区| 日本一区二区三区在线网| 日韩精品无码Av一区二区| 精品一区二区三区免费观看| 中文人妻av高清一区二区| 色婷婷一区二区三区四区成人网 |