wpf 将多参数传递给 xaml 中的 CommandParameter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13384143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:12:55  来源:igfitidea点击:

Pass multi parameter to CommandParameter in xaml

c#wpf

提问by user1205398

I want to pass serval parameters to a command via commandparameter in xaml.

我想通过 xaml 中的 commandparameter 将 serval 参数传递给命令。

<i:InvokeCommandAction Command="{Binding HideLineCommand, ElementName=militaryLineAction}"
                       CommandParameter="{Binding ID, ElementName=linesSelector}"/>

In above sample, I want to pass others variables to the command beside the ID variable. How can I achieve it? Great thanks.

在上面的示例中,我想将其他变量传递给 ID 变量旁边的命令。我怎样才能实现它?万分感谢。

回答by kmatyaszek

You can use MultiBindingwith a converter.

您可以将MultiBinding转换器一起使用。

Check this example.

检查这个例子。

Let's suppose that you have Person class.

假设您有 Person 类。

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

And you want this class as your command parameter.

并且您希望这个类作为您的命令参数。

Your XAML should look like this:

您的 XAML 应如下所示:

<Button Content="Start"
                DataContext="{Binding SourceData}"
                >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <i:InvokeCommandAction Command="{Binding SendStatus, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}">
                        <i:InvokeCommandAction.CommandParameter>
                            <MultiBinding Converter="{StaticResource myPersonConverter}">
                                <MultiBinding.Bindings>
                                    <Binding Path="Name" />
                                    <Binding Path="Age" />
                                </MultiBinding.Bindings>
                            </MultiBinding>
                        </i:InvokeCommandAction.CommandParameter>
                    </i:InvokeCommandAction>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>

Where SourceDatais a Person object.

SourceDataPerson 对象在哪里。

And myPersonConverteris a PersonConverter object.

并且myPersonConverter是一个 PersonConverter 对象。

public class PersonConverter : IMultiValueConverter
    {   
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values != null && values.Length == 2)
            {
                string name = values[0].ToString();
                int age = (int)values[1];

                return new Person { Name = name, Age = age }; 
            }
            return null;
        }

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

And in your command you can use Person object as parameter:

在您的命令中,您可以使用 Person 对象作为参数:

    public ICommand SendStatus { get; private set; }
    private void OnSendStatus(object param)
    {
        Person p = param as Person;
        if (p != null)
        {

        }
    }