WPF User Control ToolTip Using Custom Control Property Value

In a WPF application, I have user 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); } 

Problem: ToolTip always shows the value of the ControlStatus property that was at the time the OnApplyTemplate() method was OnApplyTemplate() .
The ControlStatus property of the custom control has been changed at run time, but ToolTip still always shows the initial value.

How can I make the Custom Control tooltip always show the current value of the Custom Control property?

+4
source share
1 answer

You need to use binding instead of statically setting the tooltip using ToolTipService.SetToolTip . In your case, it should be like this:

 SetBinding(ToolTipProperty, new Binding { Source = this, Path = new PropertyPath("ControlStatus"), StringFormat = "Status: {0}" }); 
+5
source

Source: https://habr.com/ru/post/1344490/


All Articles