C# 多个命令参数 wpf 按钮对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15952812/
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-08-10 18:36:02  来源:igfitidea点击:

Multiple command parameters wpf button object

c#wpfbuttonmvvmicommand

提问by Mandar Jogalekar

How can I send multiple parameters from Buttonin WPF? I am able to send single parameter which is value of TextBoxproperly. Here is the code.

如何从Buttonin发送多个参数WPF?我能够发送TextBox正确的值的单个参数。这是代码。

XAML

XAML

<TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="133,22,0,0"     Name="textBox1" VerticalAlignment="Top" Width="120" />
<Button Content="Button" Grid.Row="1" Height="23" Command="{Binding Path=CommandClick}" CommandParameter="{Binding Text,ElementName=textBox1}" HorizontalAlignment="Left" Margin="133,62,0,0" Name="button1" VerticalAlignment="Top" Width="75" />

Code behind

Code behind

public ICommand CommandClick { get; set; }

this.CommandClick = new DelegateCommand<object>(AddAccount);

private void AddAccount(object obj)
{
    //custom logic
}

采纳答案by Reed Copsey

How can i send multiple parameters from button in wpf.

如何从 wpf 中的按钮发送多个参数。

You can only send one parameter as the CommandParameter.

您只能发送一个参数作为CommandParameter.

A better solution is typically to just bind the TextBoxand other controls to multiple properties in your ViewModel. The command would then have access to all of those properties (since it's in the same class), with no need for a command parameter at all.

更好的解决方案通常是将TextBox和其他控件绑定到 ViewModel 中的多个属性。然后该命令可以访问所有这些属性(因为它在同一个类中),根本不需要命令参数。

回答by Ron.B.I

Other than using the approach of defining properties in you class (let's call it your ViewModel) to be binded by your view, there are times (not common) where we don't wan't to do so, an important tool to know in these situations is the MultiBinding, so just for completeness sake , even though you are satisfied with the first option, I'll cover another approach.

除了使用在您的类中定义属性的方法(我们称之为您的 ViewModel)由您的视图绑定之外,有些时候(不常见)我们不想这样做,这是一个重要的工具这些情况是 MultiBinding,所以为了完整起见,即使您对第一个选项感到满意,我也会介绍另一种方法。

so to answer your question:

所以回答你的问题:

1. MVVM Approach :

1. MVVM 方法:

Use the MVVM approach and define properties to binded by your view, and use those properties in your ViewModel's command without the need for CommandParameters.

使用 MVVM 方法并定义要由您的视图绑定的属性,并在您的 ViewModel 命令中使用这些属性,而无需 CommandParameters。

2. MultiBinding :(Can live happily with MVVM approach)

2. MultiBinding :(可以愉快地使用MVVM方法)

Passing the Command Parameter as a Multi Binded parameter as seen here:

将命令参数作为多绑定参数传递,如下所示:

 <Button Content="MultiBindingExample" Command="{Binding MyCommand}">
     <Button.CommandParameter>
         <MultiBinding Converter="{StaticResource MyMultiConverter}">
             <Binding Path="..." ElementName="MyTextBox"/>
             <Binding Path="..." ElementName="MySomethingElse"/>
         </MultiBinding>
     </Button.CommandParameter>
 </Button>

With your Converter Defined using the IMultiValueConverterInterface:

使用IMultiValueConverter接口定义转换器:

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

and for extracting the values: Simply refer to the parameter in your command as an Object[]and use the parameters in the same order as in the MultiBinding.

以及提取值:只需将命令中的参数称为 anObject[]并按照与 MultiBinding 中相同的顺序使用参数。