是否可以使用两个对象作为命令参数? [英] IS it possible to use two objects as command parameter?

查看:70
本文介绍了是否可以使用两个对象作为命令参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个自定义命令,该命令在两个网格之间切换.现在我想制作一种命令模板,作为参数我想使用两个网格?
有什么方法可以实现?

谢谢Moti

Hi,
I have a custom command, which switches between two grids. Now i want to make a sort of command template, an as parameters i want to use the two grids?
is there any way for realizing this?

thanks Moti

推荐答案

首先,如果您正在执行MVVM,通常可以通过从视图绑定的单独属性将这些信息提供给VM.这省去了将所有参数传递给命令的麻烦.

但是,您也可以多重绑定并使用转换器来创建参数:
首先,如果您正在执行MVVM,则通常可以通过从视图绑定的单独属性将这些信息提供给VM.这省去了将所有参数传递给命令的麻烦.

Firstly, if you are doing MVVM you would typically have this information available to your VM via separate properties bound from the view. That saves you having to pass any parameters at all to your commands.

However, you could also multi-bind and use a converter to create the parameters:
Firstly, if you''re doing MVVM you would typically have this information available to your VM via separate properties bound from the view. That saves you having to pass any parameters at all to your commands.

<button content="Zoom" command="{Binding MyViewModel.ZoomCommand" commandparameter="{Binding" elementname="MyCanvas," path="Width}"<br" mode="hold" />    <button.commandparameter>
        <multibinding converter="{StaticResource YourConverter}">
             <binding path="Width" elementname="MyCanvas" />
             <binding path="Height" elementname="MyCanvas" />
        </multibinding>
    </button.commandparameter>



在您的转换器中:



In your converter:

public class YourConverter : IMultiValueConverter
{
    public object Convert(object[] values, ...)
    {
        return values;
    }
    ...
}



然后,在您的命令执行逻辑中:



Then, in your command execution logic:

public void OnExecute(object parameter)
{
    var values = (object[])parameter;
    var width = (double)values[0];
    var height = (double)values[1];
}


这篇关于是否可以使用两个对象作为命令参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆