[英]ToolTip of a WPF Custom Control with Custom Control's property value
In a WPF application I have a Custom Control. 在WPF应用程序中,我有一个自定义控件。
public class MyControl : Control
{
static MyControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
}
public static readonly DependencyProperty ControlStatusProperty = DependencyProperty.Register("ControlStatus", typeof(int), typeof(MyControl), new PropertyMetadata(16));
public int ControlStatus
{
get
{
return (int)GetValue(ControlStatusProperty);
}
set
{
SetValue(ControlStatusProperty, value);
ChangeVisualState(false);
}
}
...
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
...
ToolTipService.SetToolTip(this, "Status: " + ControlStatus);
}
private void ChangeVisualState(bool useTransitions)
{
...
ToolTipService.SetToolTip(this, "Status: " + ControlStatus);
}
The problem is: ToolTip always shows the value of ControlStatus
property which had been at the moment of OnApplyTemplate()
method execution. 问题是:ToolTip始终显示
ControlStatus
属性的值,该值在OnApplyTemplate()
方法执行时。
The ControlStatus
property of a custom control have been changed during run-time, but the ToolTip still always shows initial value. 自定义控件的
ControlStatus
属性在运行时已更改,但ToolTip仍始终显示初始值。
How could I make the ToolTip of a Custom Control always show the current value of Custom Control's property? 如何使自定义控件的工具提示始终显示自定义控件属性的当前值?
You need to use binding instead of statically setting the tool tip with ToolTipService.SetToolTip
. 您需要使用绑定而不是使用
ToolTipService.SetToolTip
静态设置工具提示。 In your case it should be like this: 在你的情况下它应该是这样的:
SetBinding(ToolTipProperty, new Binding
{
Source = this,
Path = new PropertyPath("ControlStatus"),
StringFormat = "Status: {0}"
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.