WPF自定义控件的ToolTip MultiBinding问题
当我在 WPF 自定义控件中设置工具提示绑定时,这种方式效果完美:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
...
SetBinding(ToolTipProperty, new Binding
{
Source = this,
Path = new PropertyPath("Property1"),
StringFormat = "ValueOfProp1: {0}"
});
}
但是当我尝试使用 MultiBinding 在工具提示中包含多个属性时,它不起作用:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
...
MultiBinding multiBinding = new MultiBinding();
multiBinding.StringFormat = "ValueOfProp1: {0}\nValueOfProp2: {1}\nValueOfProp3: {2}\n";
multiBinding.Bindings.Add(new Binding
{
Source = this,
Path = new PropertyPath("Property1")
});
multiBinding.Bindings.Add(new Binding
{
Source = this,
Path = new PropertyPath("Property2")
});
multiBinding.Bindings.Add(new Binding
{
Source = this,
Path = new PropertyPath("Property3")
});
this.SetBinding(ToolTipProperty, multiBinding);
}
在这种情况下,我根本没有显示任何工具提示。
我哪里错了?
When I set a ToolTip Binding In a WPF Custom Control, this way it works perfect:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
...
SetBinding(ToolTipProperty, new Binding
{
Source = this,
Path = new PropertyPath("Property1"),
StringFormat = "ValueOfProp1: {0}"
});
}
But when I try to use MultiBinding to have several properties in the ToolTip, it doesn't work:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
...
MultiBinding multiBinding = new MultiBinding();
multiBinding.StringFormat = "ValueOfProp1: {0}\nValueOfProp2: {1}\nValueOfProp3: {2}\n";
multiBinding.Bindings.Add(new Binding
{
Source = this,
Path = new PropertyPath("Property1")
});
multiBinding.Bindings.Add(new Binding
{
Source = this,
Path = new PropertyPath("Property2")
});
multiBinding.Bindings.Add(new Binding
{
Source = this,
Path = new PropertyPath("Property3")
});
this.SetBinding(ToolTipProperty, multiBinding);
}
In this case I have no ToolTip shown at all.
Where am I wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,
MultiBinding
上的StringFormat
仅适用于string
类型的属性,而ToolTip
属性的类型为对象
。在这种情况下,MultiBinding
需要定义一个值转换器。作为解决方法,您可以将
TextBlock
设置为ToolTip
并使用MultiBinding
绑定其Text
属性(因为>Text
的类型为string
,它将与StringFormat
一起使用):It turns out that
StringFormat
onMultiBinding
works only on properties of typestring
, while theToolTip
property is of typeobject
. In this case theMultiBinding
requires a value converter defined.As a workaround you could set a
TextBlock
as aToolTip
and bind itsText
property usingMultiBinding
(sinceText
is of typestring
it'll work withStringFormat
):