CommandParameter传递给Command写法
时间: 2023-11-27 19:52:52 浏览: 96
在WPF或者Silverlight中,CommandParameter可以被用来传递额外的数据给Command。以下是一些写法:
1. 直接绑定到一个值
```
<Button Content="Click me" Command="{Binding MyCommand}" CommandParameter="Hello World"/>
```
2. 绑定到控件的某个属性
```
<TextBox x:Name="myTextBox" Text="Some text"/>
<Button Content="Click me" Command="{Binding MyCommand}" CommandParameter="{Binding ElementName=myTextBox, Path=Text}"/>
```
3. 使用MultiBinding传递多个值
```
<TextBox x:Name="firstNameTextBox" Text="John"/>
<TextBox x:Name="lastNameTextBox" Text="Doe"/>
<Button Content="Click me" Command="{Binding MyCommand}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource myConverter}">
<Binding ElementName="firstNameTextBox" Path="Text"/>
<Binding ElementName="lastNameTextBox" Path="Text"/>
</MultiBinding>
</Button.CommandParameter>
</Button>
```
在上面的例子中,我们使用了一个名为myConverter的转换器,它将两个参数合并成一个字符串。在Command的Execute方法中,我们可以使用一个object[]数组来接收这些参数。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)