我正在尝试将路由事件添加到控件中已存在的工具提示中 - 现在它绑定到简单的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);
}
}
问题是当工具提示发生变化时如何触发此事件?
答案 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;
}