多个命令参数的WPF按钮对象 [英] Multiple command parameters wpf button object

查看:140
本文介绍了多个命令参数的WPF按钮对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何发送从按钮多个参数的 WPF ?我能够发送单一的参数是文本框的值正常。这里是code。

XAML

 <文本框Grid.Row =1HEIGHT =23的Horizo​​ntalAlignment =左保证金=133,22,0,0NAME =textBox1的VerticalAlignment = 顶级WIDTH =120/>
<按钮内容=按钮Grid.Row =1HEIGHT =23命令={绑定路径= CommandClick}CommandParameter ={结合文本,的ElementName = textBox1的}的Horizo​​ntalAlignment =左保证金= 133,62,0,0NAME =Button1的VerticalAlignment =评出的WIDTH =75/>

code后面

 公共ICommand的CommandClick {搞定;组; }this.CommandClick =新DelegateCommand<对象>(AddAccount);私人无效AddAccount(obj对象)
{
    //自定义逻辑
}


解决方案

  

如何从按钮在WPF发送多个参数。


您只能发送一个参数为 CommandParameter

一个更好的解决方案通常只绑定文本框和其他控制在你的ViewModel多​​个属性。然后,命令将有机会获得所有这些属性的(因为它是在同一个班),没有必要在所有的命令参数。

How can I send multiple parameters from Button in WPF? I am able to send single parameter which is value of TextBox properly. Here is the code.

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

public ICommand CommandClick { get; set; }

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

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

解决方案

How can i send multiple parameters from button in wpf.

You can only send one parameter as the CommandParameter.

A better solution is typically to just bind the TextBox and 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.

这篇关于多个命令参数的WPF按钮对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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