今晚这也使我发疯。我创建了一个ToolTip
子类来处理该问题。对我来说,在.NET 4.0上,该ToolTip.StaysOpen
属性不是“真正”保持打开状态。
在下面的类中,使用new属性ToolTipEx.IsReallyOpen
而不是property ToolTip.IsOpen
。您将获得所需的控件。通过Debug.Print()
调用,您可以在调试器的“输出”窗口中查看调用了多少次this.IsOpen = false
!那么多StaysOpen
,还是我应该说"StaysOpen"
?请享用。
public class ToolTipEx : ToolTip
{
static ToolTipEx()
{
IsReallyOpenProperty =
DependencyProperty.Register(
"IsReallyOpen",
typeof(bool),
typeof(ToolTipEx),
new FrameworkPropertyMetadata(
defaultValue: false,
flags: FrameworkPropertyMetadataOptions.None,
propertyChangedCallback: StaticOnIsReallyOpenedChanged));
}
public static readonly DependencyProperty IsReallyOpenProperty;
protected static void StaticOnIsReallyOpenedChanged(
DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ToolTipEx self = (ToolTipEx)o;
self.OnIsReallyOpenedChanged((bool)e.OldValue, (bool)e.NewValue);
}
protected void OnIsReallyOpenedChanged(bool oldValue, bool newValue)
{
this.IsOpen = newValue;
}
public bool IsReallyOpen
{
get
{
bool b = (bool)this.GetValue(IsReallyOpenProperty);
return b;
}
set { this.SetValue(IsReallyOpenProperty, value); }
}
protected override void OnClosed(RoutedEventArgs e)
{
System.Diagnostics.Debug.Print(String.Format(
"OnClosed: IsReallyOpen: {0}, StaysOpen: {1}", this.IsReallyOpen, this.StaysOpen));
if (this.IsReallyOpen && this.StaysOpen)
{
e.Handled = true;
// We cannot set this.IsOpen directly here. Instead, send an event asynchronously.
// DispatcherPriority.Send is the highest priority possible.
Dispatcher.CurrentDispatcher.BeginInvoke(
(Action)(() => this.IsOpen = true),
DispatcherPriority.Send);
}
else
{
base.OnClosed(e);
}
}
}
小问题:Microsoft为什么不将DependencyProperty
属性(获取程序/设置程序)虚拟化,以便我们可以接受/拒绝/调整子类中的更改?还是virtual OnXYZPropertyChanged
每个都做一个DependencyProperty
?啊。
- -编辑 - -
我上面的解决方案在XAML编辑器中看起来很奇怪-工具提示始终显示,从而阻止了Visual Studio中的某些文本!
这是解决此问题的更好方法:
一些XAML:
<!-- Need to add this at top of your XAML file:
xmlns:System="clr-namespace:System;assembly=mscorlib"
-->
<ToolTip StaysOpen="True" Placement="Bottom" HorizontalOffset="10"
ToolTipService.InitialShowDelay="0" ToolTipService.BetweenShowDelay="0"
ToolTipService.ShowDuration="{x:Static Member=System:Int32.MaxValue}"
>This is my tooltip text.</ToolTip>
一些代码:
// Alternatively, you can attach an event listener to FrameworkElement.Loaded
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
// Be gentle here: If someone creates a (future) subclass or changes your control template,
// you might not have tooltip anymore.
ToolTip toolTip = this.ToolTip as ToolTip;
if (null != toolTip)
{
// If I don't set this explicitly, placement is strange.
toolTip.PlacementTarget = this;
toolTip.Closed += new RoutedEventHandler(OnToolTipClosed);
}
}
protected void OnToolTipClosed(object sender, RoutedEventArgs e)
{
// You may want to add additional focus-related tests here.
if (this.IsKeyboardFocusWithin)
{
// We cannot set this.IsOpen directly here. Instead, send an event asynchronously.
// DispatcherPriority.Send is the highest priority possible.
Dispatcher.CurrentDispatcher.BeginInvoke(
(Action)delegate
{
// Again: Be gentle when using this.ToolTip.
ToolTip toolTip = this.ToolTip as ToolTip;
if (null != toolTip)
{
toolTip.IsOpen = true;
}
},
DispatcherPriority.Send);
}
}
结论:类ToolTip
和有所不同ContextMenu
。两者都有管理某些属性的“服务”类(例如ToolTipService
和)ContextMenuService
,并且Popup
在显示期间均用作“秘密”父控件。最后,我注意到Web上的所有 XAML ToolTip示例都没有ToolTip
直接使用类。相反,它们嵌入一个StackPanel
带有TextBlock
秒。你说的话:“嗯...”
ShowDuration
,认为它类似于30,000
。大于此值的值将默认恢复为5000
。