笔记1:WPFCommand命令使用

创建BaseCommand实现Icommand接口

public class BaseCommand : ICommand
    {
        public Action<object> ExecuteAction;
        private BaseCommand filterCommand;

        public BaseCommand(Action<object> action)
        {
            ExecuteAction = action;
        }

        public BaseCommand(BaseCommand filterCommand)
        {
            this.filterCommand = filterCommand;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }
        public event EventHandler CanExecuteChanged;
        public void Execute(object parameter)
        {
            ExecuteAction(parameter);
        }
    }

创建ViewModel实现INotifyPropertyChanged接口

public class ViewModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// UI通知
        /// </summary>
        /// <param name="PropertyName"></param>
        public void OnPropertyChanged([CallerMemberName]string PropertyName="")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
        }


       /// <summary>
        /// Command Button点击命令
        /// </summary>
        public BaseCommand But_click
        {
            get
            {
               return  new BaseCommand(o=> {
					//逻辑代码
                });
            }
        }
    }

xaml代码

	<Grid>
        <Button Command="{Binding But_click}" >
            点击
        </Button>
    </Grid>
 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //将viewmodel绑定为窗体的数据源
            this.DataContext = new ViewModel();
        }
    }

传递参数

ViewModel

public BaseCommand But_click
        {
            get
            {
               return  new BaseCommand(o=> {
                   MessageBox.Show(o.GetType().ToString());
                });
            }
}

xaml,直接传递string类型的参数

<Button Command="{Binding But_click}" CommandParameter="66">
            点击
</Button>

效果图

xaml,传递其他控件的相应属性

<Label Name="image_nasme" Content="你找到我了"/>
        <Button Command="{Binding But_click}" 
        CommandParameter="{Binding Path=Content,ElementName=image_nasme}" >
            点击
</Button>

ViewModel

public BaseCommand But_click
        {
            get
            {
               return  new BaseCommand(o=> {
                   MessageBox.Show(o.GetType().ToString()+"--->"+o.ToString());
                });
            }
}

xaml,传递控件自己(RelativeSource={x:Static RelativeSource.Self}}表示绑定自己)

<Label Name="image_nasme" Content="你找到我了"/>
        <Button Command="{Binding But_click}" 
        CommandParameter="{Binding Path=Content,ElementName=image_nasme}" >
            点击
</Button>

ViewModel

public BaseCommand But_click
        {
            get
            {
               return  new BaseCommand(o=> {
                   MessageBox.Show(o.GetType().ToString()+"--->"+((Button)o).Content);
                });
            }
}

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值