具有自定义控件属性值的 WPF 自定义控件的工具提示
在 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);
}
问题是:工具提示始终显示 ControlStatus
属性的值,该值是在 OnApplyTemplate()
方法执行时的值。
自定义控件的 ControlStatus
属性在运行时已更改,但工具提示仍然始终显示初始值。
如何使自定义控件的工具提示始终显示自定义控件属性的当前值?
In a WPF application I have a Custom Control.
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.
The ControlStatus
property of a custom control have been changed during run-time, but the ToolTip still always shows initial value.
How could I make the ToolTip of a Custom Control always show the current value of Custom Control's property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用绑定,而不是使用
ToolTipService.SetToolTip
静态设置工具提示。在你的情况下应该是这样的:You need to use binding instead of statically setting the tool tip with
ToolTipService.SetToolTip
. In your case it should be like this: