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);
}
问题是:ToolTip始终显示ControlStatus属性的值,该值在OnApplyTemplate()方法执行时.
自定义控件的ControlStatus属性在运行时已更改,但ToolTip仍始终显示初始值.
解决方法:
您需要使用绑定而不是使用ToolTipService.SetToolTip静态设置工具提示.在你的情况下它应该是这样的:
SetBinding(ToolTipProperty, new Binding
{
Source = this,
Path = new PropertyPath("ControlStatus"),
StringFormat = "Status: {0}"
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。