我有一个自定义ICommand类,正在尝试向其中添加属性“ DisplayName”和“ ToolTipText”:
internal class MyCommand : ICommand
{
// ...
internal string DisplayName {
get { return displayName; }
set {
if (displayName == value)
return;
if (value == null)
value = "";
displayName = value;
OnPropertyChanged(nameof(DisplayName));
}
}
internal string ToolTipText {
get { return toolTipText; }
set {
if (toolTipText == value)
return;
if (value == null)
value = "";
toolTipText = value;
OnPropertyChanged(nameof(ToolTipText));
}
}
// ...
}
我的ViewModel返回ICommand的自定义类型:
public MyCommand MyButtonCommand {
get {
return myButtonCommand?? (myButtonCommand = new MyCommand (
// ...
);
}
}
在XAML中,我将命令绑定到Button,并且我想从Command的DisplayName中设置按钮Content。根据答案的要求,按钮绑定是这样的:
<Button HorizontalAlignment="Right" VerticalAlignment="Center"
Margin="12,0,0,0" DockPanel.Dock="Right" TabIndex="10"
Command="{Binding MyButtonCommand, Mode=OneWay}"/>
结果如下:
Content="{Binding Command, RelativeSource={RelativeSource Self}}"
这样,按钮的内容是Command上的ToString()。但是我无法访问自定义ICommand对象的属性。
像这样的XAML语法不起作用:
Content="{Binding Command.DisplayName, RelativeSource={RelativeSource Self}}"
Content="{Binding Command/DisplayName, RelativeSource={RelativeSource Self}}"
有什么答案吗?
...现在回答:仅需要将我的DisplayName和ToolTipText属性公开...
以下xaml均有效
古典的,标准的
Content="{Binding MyButtonCommand.DisplayName}" Command="{Binding MyButtonCommand}"
非常不寻常
`Content="{Binding Command.DisplayName, RelativeSource={RelativeSource Self}}" Command="{Binding MyButtonCommand}`"
假定MyCommand和DisplayName都是公共的而不是内部的
public class MyCommand : ICommand
{
public string DisplayName
并且DisplayName有一个值...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句