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

    下載app免費(fèi)領(lǐng)取會(huì)員

    NULL

    ad.jpg

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

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

    網(wǎng)友投稿

    更多

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


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


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


    首先現(xiàn)有的Button肯定是不行了,所以自己寫一個(gè)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);

                        }

                    }

                }

            }

        }


    再寫一個(gè)命令


        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视频一区二区| 在线观看国产一区亚洲bd| 国产一区二区在线观看麻豆| 日韩精品人妻一区二区三区四区| 天海翼一区二区三区高清视频| 在线观看国产一区亚洲bd| 一区二区精品在线| 午夜精品一区二区三区在线视| 日韩一区二区三区视频久久| 欧美人妻一区黄a片| 2018高清国产一区二区三区| 成人影片一区免费观看| 一区二区国产在线观看| 精品在线一区二区| 国产成人久久一区二区三区| 国语对白一区二区三区| 日韩精品免费一区二区三区| 无码喷水一区二区浪潮AV| 亚洲中文字幕一区精品自拍| 国产精品高清一区二区三区不卡| 农村人乱弄一区二区| 国产精品区一区二区三在线播放| 一区二区三区中文| 一区二区三区在线观看中文字幕| 成人在线一区二区| 久久久久久综合一区中文字幕| tom影院亚洲国产一区二区| 无码人妻品一区二区三区精99 | 丝袜人妻一区二区三区| 无码精品前田一区二区| 无码少妇一区二区三区芒果| 国产日韩精品一区二区在线观看播放 | 中文乱码精品一区二区三区| 亚洲国产精品一区二区第一页免 | 日本一区二区三区免费高清在线| 天码av无码一区二区三区四区| 武侠古典一区二区三区中文| 中文日韩字幕一区在线观看| 无码囯产精品一区二区免费| 国产精品va无码一区二区| 天堂不卡一区二区视频在线观看|