Command 傳參的幾種方式

Command能夠根據CommandParameter傳參git

關鍵代碼

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>

也能夠這樣綁定github

<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>

示例代碼

示例代碼c#

Command與Click的關係

Command用於後臺操做,click用於前臺展現、交互,能夠共存code

相關文章
相關標籤/搜索