更改工具提示时触发路由事件

本文关键字:路由 事件 工具提示 | 更新日期: 2023-09-27 18:01:28

我试图在控件中添加路由事件到现有的工具提示-现在它被绑定到简单的get/set属性

binding in xaml:

 <Style x:Key="Style" TargetType="{x:Type myControl}">
      <Setter Property="ToolTip" Value="{Binding Name}" />

prop in myControl:

 public string Name
    {
      get;
      set;
    }

控制类中的路由事件,包含myControls列表(我想我写得对)

public static readonly RoutedEvent TooltipChangedEvent = EventManager.RegisterRoutedEvent("ToolTipChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<string>), typeof(Control));
    public event RoutedPropertyChangedEventHandler<string> ToolTipChanged
    {
        add
        {
            AddHandler(TooltipChangedEvent, value);
        }
        remove
        {
            RemoveHandler(TooltipChangedEvent, value);
        }
    }

问题是当工具提示改变时如何触发此事件?

更改工具提示时触发路由事件

class MainViewModel : FrameworkElement, INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            RaiseEvent(new RoutedEventArgs(TooltipChangedEvent));
            OnPropertyChanged();
        }
    }
    public static readonly RoutedEvent TooltipChangedEvent = EventManager.RegisterRoutedEvent("ToolTipChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<string>), typeof(Control));
    public event RoutedPropertyChangedEventHandler<string> ToolTipChanged
    {
        add
        {
            AddHandler(TooltipChangedEvent, value);
        }
        remove
        {
            RemoveHandler(TooltipChangedEvent, value);
        }
    }
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}