【发布时间】:2017-11-08 16:26:06
【问题描述】:
我有一个TextBox 绑定到ViewModel 上的string 属性,我有一个Button 和Command。现在,如果可能,我想将属性本身作为CommandParameter 传递。
这可能吗?
Xaml 部分:
<TextBox Text="{Binding FilePath, UpdateSourceTrigger=PropertyChanged}"/>
<Button Command="{Binding BrowseCommand}" CommandParameter="{Binding FilePath}" Content="..." />
Command 看起来像这样,但是我必须放置什么类型而不是 RelayCommand<?>,我需要将 CommandParameter 绑定到什么?
public ICommand BrowseCommand => this.browseCommand ?? (this.browseCommand = new RelayCommand<?>(this.Browse));
【问题讨论】:
-
文件路径是我假设的字符串类型?
-
是的,我想传递属性本身,而不是它的值(如果可能的话)
-
这是不可能的。 {Binding} 解析 值。
-
出于好奇,您为什么要引用该属性?也许还有另一种方式。描述你的情况。
-
感谢您的帮助。所以我有多个
TextBoxes,每个都绑定到自己的属性。对于每个TextBox,我都有一个Button,它应该设置适当TextBox的属性。我想对所有Buttons 使用一个命令,我能想到的唯一方法是Command需要知道它应该设置的属性。我希望它变得清晰:#
标签: c# wpf xaml properties binding