假设我有一个ToolTip在XAML中指定的样式,如下所示:
<Button Content="Click me" ToolTip="Likes to be clicked">
<Button.Resources>
<Style TargetType="{x:Type ToolTip}" BasedOn="{StaticResource {x:Type ToolTip}}">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="HasDropShadow" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToolTip}">
<StackPanel Background="Wheat" Height="200" Width="200">
<TextBlock x:Name="TxbTitle" FontSize="24" Text="ToolTip" Background="BurlyWood" />
<ContentPresenter />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Resources>
</Button>
Run Code Online (Sandbox Code Playgroud)
鉴于我的一个参考Button,而且ToolTip显示时,我怎么能找到Popup的ToolTip(后来找其视觉儿童,例如TxbTitle)?
更新:
基于pushpraj的回答,我能够抓住(完整)可视化树,它看起来像这样:
System.Windows.Controls.Primitives.PopupRoot
System.Windows.Controls.Decorator
System.Windows.Documents.NonLogicalAdornerDecorator
System.Windows.Controls.ToolTip
System.Windows.Controls.StackPanel
System.Windows.Controls.TextBlock (TxbTitle)
System.Windows.Controls.ContentPresenter
System.Windows.Controls.TextBlock
System.Windows.Documents.AdornerLayer
Run Code Online (Sandbox Code Playgroud)
在这里我可以找到TxbTitle TextBlock.
(像这样的逻辑树:)
System.Windows.Controls.Primitives.Popup
System.Windows.Controls.ToolTip
System.String
Run Code Online (Sandbox Code Playgroud)
然而,pushpraj的答案是基于我可以掌握ToolTip实例.我得到的是Button唯一的,Button.ToolTip属性返回字符串"Likes to be clicked",而不是ToolTip实例.
所以更具体地说,问题是,我可以得到保持ToolTip 或在Popup以某种方式时,我的一切是Button.
(疯狂的想法:有没有办法枚举所有开放Popup的?)
A ToolTip是一种Popup托管工具提示内容的类型
由于Popup托管在一个单独的窗口中,因此它拥有自己的逻辑和可视树
以下信息是工具提示的可视和逻辑树
视觉树
System.Windows.Controls.Primitives.PopupRoot
System.Windows.Controls.Decorator
System.Windows.Documents.NonLogicalAdornerDecorator
System.Windows.Controls.ToolTip
Run Code Online (Sandbox Code Playgroud)
逻辑树
System.Windows.Controls.Primitives.Popup
System.Windows.Controls.ToolTip
Run Code Online (Sandbox Code Playgroud)
注意:由于弹出窗口具有自己的根,因此可能无法从主窗口的可视或逻辑树访问它.
找到工具提示的弹出窗口
我使用附加属性来查找工具提示的弹出窗口
namespace CSharpWPF
{
public class ToolTipHelper : DependencyObject
{
public static bool GetIsEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsEnabledProperty);
}
public static void SetIsEnabled(DependencyObject obj, bool value)
{
obj.SetValue(IsEnabledProperty, value);
}
// Using a DependencyProperty as the backing store for IsEnabled. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(ToolTipHelper), new PropertyMetadata(false,OnEnable));
private static void OnEnable(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ToolTip t = d as ToolTip;
DependencyObject parent = t;
do
{
parent = VisualTreeHelper.GetParent(parent);
if(parent!=null)
System.Diagnostics.Debug.Print(parent.GetType().FullName);
} while (parent != null);
parent = t;
do
{
//first logical parent is the popup
parent = LogicalTreeHelper.GetParent(parent);
if (parent != null)
System.Diagnostics.Debug.Print(parent.GetType().FullName);
} while (parent != null);
}
}
}
Run Code Online (Sandbox Code Playgroud)
XAML
<Button Content="Click me" ToolTip="Likes to be clicked">
<Button.Resources>
<Style TargetType="{x:Type ToolTip}" BasedOn="{StaticResource {x:Type ToolTip}}"
xmlns:l="clr-namespace:CSharpWPF">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="HasDropShadow" Value="True" />
<Setter Property="l:ToolTipHelper.IsEnabled" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToolTip}">
<StackPanel Background="Wheat" Height="200" Width="200">
<TextBlock x:Name="TxbTitle" FontSize="24" Text="ToolTip" Background="BurlyWood" />
<ContentPresenter />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Resources>
</Button>
Run Code Online (Sandbox Code Playgroud)
我已将新创建的附加属性添加到工具提示样式 <Setter Property="l:ToolTipHelper.IsEnabled" Value="True"/>
从后面的代码中检索ToolTip实例
如果您无法从xaml指定样式的样式或模板,则后面的代码是检索工具提示实例的方法
示例代码
Style style = new Style(typeof(ToolTip), (Style)this.FindResource(typeof(ToolTip)));
style.Setters.Add(new Setter(ToolTipHelper.IsEnabledProperty, true));
this.Resources.Add(typeof(ToolTip), style);
Run Code Online (Sandbox Code Playgroud)
上面的代码为工具提示创建了一个样式对象,并为ToolTipHelper.IsEnabledProperty窗口的资源添加了一个setter 并将相同的样式注入
因此,当需要显示工具提示时,将OnEnable在ToolTipHelper类中调用属性更改处理程序.并且处理程序中的依赖项对象将是您可能进一步操作的实际工具提示实例.
| 归档时间: |
|
| 查看次数: |
1853 次 |
| 最近记录: |