zoukankan      html  css  js  c++  java
  • WPF委托命令DelegateCommand的传参方式

    首先引用  Microsoft.Practices.Prism

    MVVM模式代码如下:

    XAML代码:

    <!-- 无参方式 -->
    <Button Content="Test Command" Command="{Binding TestCommand}" />
    
    <!-- 将自己作为参数 -->
    <Button Content="Test Command2" Command="{Binding TestCommand2}" CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}" >
    
    <!-- 将父元素作为参数 -->
    <Button Content="Test Command3" Command="{Binding TestCommand3}" CommandParameter="{Binding RelativeSource={x:Static RelativeSource.TemplatedParent}}" >

    后台代码:

    // code-behind构造函数中添加:
    
    this.DataContext = new ViewModel();

    ViewModel代码:

    // ViewModel 构造函数
    public ViewModel()
    {    CallCOmmand1 = new DelegateCOmmmand(Call1);
        CallCOmmand2 = new DelegateCOmmmand<Object>(Call2);
        CallCOmmand3 = new DelegateCOmmmand<Object>(Call3);
    }
    
    // 命令声明
    public DelegateCommand CallCommand { get; private set; }
    public DelegateCommand<Object> CallCommand2 { get; private set; }
    public DelegateCommand<Object> CallCommand3 { get; private set; }
    
    // 命令实现
    public void Call1()
    {
    }
    
    public void Call2( Object obj )
    {
        Button button = obj as Button;
    }
    
    public void Call3( Object obj )
    {
        ParentType parent = obj as ParentType;
    }
  • 相关阅读:
    linux网卡混杂模式打开
    Python解释器运行成功,命令运行显示无此属性解决办法
    Python-01基础-09Python相关术语
    Python-01基础-05模块
    Python-01基础-04类
    Python-01基础-03函数
    Python-01基础-02数据类型
    Python-01基础-01Python简介
    Python-01基础-00菜鸟教程
    Module-Dask并行任务调度
  • 原文地址:https://www.cnblogs.com/bincoding/p/8288677.html
Copyright © 2011-2022 走看看