多个命令参数的WPF按钮对象 [英] Multiple command parameters wpf button object
问题描述
我如何发送从按钮
多个参数的 WPF
?我能够发送单一的参数是文本框的值
正常。这里是code。
XAML
<文本框Grid.Row =1HEIGHT =23的HorizontalAlignment =左保证金=133,22,0,0NAME =textBox1的VerticalAlignment = 顶级WIDTH =120/>
<按钮内容=按钮Grid.Row =1HEIGHT =23命令={绑定路径= CommandClick}CommandParameter ={结合文本,的ElementName = textBox1的}的HorizontalAlignment =左保证金= 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
inWPF
? I am able to send single parameter which is value ofTextBox
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屋!