Command 传参的几种方式


Command可以根据CommandParameter传参

关键代码

public ICommand SubmitCommand => _submitCommand;
private RelayCommand _submitCommand = new RelayCommand(new Action<object>(ShowMessage));
private static void ShowMessage(object obj)
{
    MessageBox.Show(obj.ToString());
}
<TextBox Name="textBox1"></TextBox>
<Button Command="{Binding SubmitCommand}" CommandParameter="{Binding ElementName=textBox1,Path=Text}" Content="提交参数"></Button>

也可以这样绑定

<Label Grid.Row="0">姓名:</Label>
<TextBox Name="name" Grid.Row="0" Grid.Column="1" Text="{Binding Person.Name}"></TextBox>
<Label Grid.Row="1">年龄:</Label>
<TextBox Name="age" Grid.Row="1" Grid.Column="1" Text="{Binding Person.Age}"></TextBox>
<Button Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Command="{Binding SubmitCommand}" CommandParameter="{Binding Person}" Content="提交参数"></Button>

示例代码

示例代码

Command与Click的关系

Command用于后台操作,click用于前台展示、交互,可以共存


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM