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

时间:2015-07-08 09:55:56

标签: c# wpf

我正在尝试将路由事件添加到控件中已存在的工具提示中 - 现在它绑定到简单的get / set属性。

xaml中的

绑定:

 <Style x:Key="Style" TargetType="{x:Type myControl}">
      <Setter Property="ToolTip" Value="{Binding Name}" />
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);
        }
    }

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

1 个答案:

答案 0 :(得分:1)

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;
}