使用IMultiValueConverter将多个CommandParameter传递给viewModel [英] Using IMultiValueConverter to pass multiple CommandParameters to viewModel

查看:45
本文介绍了使用IMultiValueConverter将多个CommandParameter传递给viewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

<DataGridTemplateColumn Header="Security">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Name="Security" Content="{Binding Path=totalSecurities}" Command="{Binding Source={StaticResource viewModel}, Path=filterGridCommand}">
                                    <Button.CommandParameter>
                                        <MultiBinding Converter="{StaticResource PassThroughConverter}">
                                            <Binding Path="sector"/>
                                            <Binding ElementName="Security" Path="Name"/>
                                        </MultiBinding>
                                    </Button.CommandParameter>

                                </Button>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

下面是PassThroughConverter的代码:

Below is the code for PassThroughConverter:

public class PassThroughConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameters, CultureInfo culture)
    {

        return values;
    }

    public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

当我击中返回值行时进行调试时,正确的值在数组中但是,当我按下触发filtergridcommand的按钮时,返回的值都为null?谁能帮忙.谢谢你.

When I debug as soon as it hits the return values line, the correct values are in the array but when i press the button that triggers the filtergridcommand, the values returned are both null? Can anyone help. Thank you.

推荐答案

一些实验证实这样做

public object Convert(object[] values, Type targetType, 
                      object parameters, CultureInfo culture)
{
    return values;
}

导致命令参数以 object [] {null,null} 结尾.

每次绑定值更改时(而不是在执行命令时)运行转换器,并且在执行命令时将返回值缓存起来以供使用.原始参数 object []值似乎被重置为所有空值.

The converter is run every time a bound value changes, not when the command is executed, and the return value is cached for use when the command is executed. The original parameter object[] values appears to be reset to all nulls.

解决方案是克隆 values 参数.您可以这样做:

The solution is to clone the values parameter. In your case you can do this:

public object Convert(object[] values, Type targetType, 
                      object parameter, CultureInfo culture)
{
    return new [] {values[0], values[1]};
}

更有用的是,可以像这样处理可变数量的值:

More usefully, a variable number of values can be handled like this:

public object Convert(object[] values, Type targetType, 
                      object parameter, CultureInfo culture)
{
    return values.ToArray();
}

这篇关于使用IMultiValueConverter将多个CommandParameter传递给viewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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