zoukankan      html  css  js  c++  java
  • WPF 通过 CommandParameter 传递当前窗体到 ViewModel

    在应用 Command 模式中,需要在View上点击 一个按钮,需要将当前窗体作为参数传递为 command


    两种方式传递当前窗体
    1、通过窗体名称(假设窗体名称为 ThisWindow)

       <Button
    Command="
    CancelCommand"CommandParameter="{Binding ElementName=ThisWindow}"/>


    2、绑定到
    RelativeSource

    <ButtonCommand="CancelCommand"CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>

    -------------------------------------------------------------------------------------------------------------------
    以下是ViewModel中的 Command 代码

            
    private DelegateCommand<Window> _cancelCommand = null;

            public DelegateCommand<Window> CancelCommand
            {
                get
                {
                    if (_cancelCommand == null)
                    {
                        _cancelCommand = new DelegateCommand<Window>(Cancel);
                    }

                    return _cancelCommand;
                }
            }
     

            void Cancel(Window window)
            {    
                if (window != null)
                {  
                    window.DialogResult = false;
                    window.Close();
                }
            }


    参考网址:http://stackoverflow.com/questions/3504332/passing-the-current-window-as-a-commandparameter

  • 相关阅读:
    php基础:函数定义和时间戳函数
    php基础:函数
    http协议基础
    laravel基础②
    laravel基础①安装、配置虚拟主机
    composer
    git基础⑤远程仓库
    windows+caffe(一)——自己环境
    caffe问题集锦
    使用cygwin出现syntax error near unexpected token'$'do
  • 原文地址:https://www.cnblogs.com/babietongtianta/p/3474101.html
Copyright © 2011-2022 走看看