WPF MVVM Multibinding-如何使用RelayCommand将两个参数传递给Command

时间:2018-07-17 16:47:48

标签: wpf mvvm command multibinding relaycommand

如何使用RelayCommand将2个参数传递给命令。我需要将COntrols作为参数(网格和窗口)传递。我完全意识到,此类问题已在Stack Overflow上存在,但我正努力根据自己的需要进行调整。

  1. 看一下我的第一次尝试。显然这是行不通的,因为中继无法获取2个参数。

Xaml代码:

        <Button Name="StretchScreenBtn" Content="Stretch screen" DataContext="   {StaticResource CommandWindow}" Command="{Binding ResizeScreenCommand}"
                Width="100" Height="50">
             <Button.CommandParameter>
                <MultiBinding Converter="{StaticResource CommandParamsConv}">
                    <Binding ElementName="ScreenGrid" />
                    <Binding RelativeSource="{RelativeSource AncestorType=Window}" /> 
                </MultiBinding> 
            </Button.CommandParameter>  
        </Button>

ViewModel中的命令代码:

        private ICommand _ResizeScreenCommand;
        public ICommand ResizeScreenCommand
        {
            get
            {
                if (_ResizeScreenCommand == null)
                {
                    _ResizeScreenCommand = new RelayCommand(
                        (argument1, argument2) =>
                        {

                            Grid grid = argument1 as Grid;
                            Window window = argument2 as Window;
                            if ((int)grid.GetValue(Grid.ColumnSpanProperty) == 2)
                            {
                                grid.SetValue(Grid.ColumnSpanProperty, 1);
                                window.WindowStyle = WindowStyle.None;
                            }
                            else
                            {
                                grid.SetValue(Grid.ColumnSpanProperty, 2);
                                window.WindowStyle = WindowStyle.SingleBorderWindow;
                            }
                        }
                        );
                }
                return _ResizeScreenCommand;
            }
        }

还有MultiValueConverter:

     class CommandParamsConverter : IMultiValueConverter
     {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            object[] parameters = values;
            return parameters;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
     }
  1. 我第二次尝试遵循How to Passing multiple parameters RelayCommand?的解决方案

因此,我在ViewModel中创建了具有两个属性Grid和Window类型的新类,然后尝试在xaml中将Elements绑定到这些属性。但是在这种情况下,编译器会抱怨这些属性是非依赖的,无法绑定。

请给我任何解决方法的提示吗?

下面是修改后的xaml代码:

        <Button Name="StretchScreenBtn" Content="Stretch screen" DataContext="{StaticResource CommandWindow}" Command="{Binding ResizeScreenCommand}"
                Width="100" Height="50">
             <Button.CommandParameter>
                <vm:StretchingModel Grid="{Binding ElementName=ScreenGrid}" Win="{Binding RelativeSource={RelativeSource AncestorType=Window}}" />
            </Button.CommandParameter>  
        </Button>

还有ViewModel中的extral类:

    class StretchingModel
    {
        public Grid Grid { get; set; }
        public Window Win { get; set; }
    } 

1 个答案:

答案 0 :(得分:0)

GridWindow传递给视图模型不是MVVM ...视图模型应该不依赖于任何UI元素。

无论如何,要将多个值传递给命令,您应该结合使用两种方法。转换器应返回StretchingModel的实例:

class CommandParamsConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return new StretchingModel() { Grid = values[0], Window = values[1] };
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

...该命令接受的内容:

private ICommand _ResizeScreenCommand;
public ICommand ResizeScreenCommand
{
    get
    {
        if (_ResizeScreenCommand == null)
        {
            _ResizeScreenCommand = new RelayCommand(
                (argument) =>
                {

                    StretchingModel model = argument as StretchingModel;
                    ...
                }
                );
        }
        return _ResizeScreenCommand;
    }
}