• 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}}" >
    

    后台代码:

    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;
    }
    

      

      

      

  • 相关阅读:
    JS高级——eval
    JS高级——Object.prototype成员
    JS高级——原型链
    JS高级——逻辑中断
    移动web——bootstrap媒体对象
    移动web——bootstrap如何修改原组件
    移动web——bootstrap响应式轮播图
    Leetcode题解
    位运算实现四则运算(C++实现)
    扫码登陆原理
  • 原文地址:https://www.cnblogs.com/sntetwt/p/11315738.html
Copyright © 2020-2023  润新知