绑定到wpf自定义控件依赖项属性以获得工具提示吗? [英] Bind to wpf custom control dependency property for tooltip?

查看:119
本文介绍了绑定到wpf自定义控件依赖项属性以获得工具提示吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用WPF编写的自定义控件,该控件具有布尔依赖性属性:

I have a custom control I wrote in WPF, which has a Boolean dependency property:

public static readonly DependencyProperty IsAlertProperty
    = DependencyProperty.Register("IsAlert", typeof(bool), typeof(AlertControl),
        new FrameworkPropertyMetadata(default(bool),
            FrameworkPropertyMetadataOptions.None,
            OnIsAlertChanged,
            null,
            false,
            UpdateSourceTrigger.PropertyChanged));

public bool IsAlert
{
    get { return (bool)GetValue(IsAlertProperty); }
    set { SetValue(IsAlertProperty, value); }
}

在我的Generic.xaml中,我有以下xaml代码:

In my Generic.xaml, I have the following xaml code:

<Style TargetType="{x:Type local:AlertControl}">
    <Setter Property="Template">
        <Setter.Value> ... </Setter.Value>
    </Setter>

    <Setter Property="ToolTip">
        <Setter.Value>
            <ToolTip Visiblity="{Binding Path=IsAlert,
                             RelativeSource={RelativeSource TemplatedParent},
                             Converter={StaticResource BooleanToVisiblityConverter}">
                <!-- Tooltip content goes here -->
            </ToolTip>
        </Setter.Value>
    </Setter>
<Style />

问题是,这似乎不起作用。我使用Snoop监视我的xaml,并且 IsAlert 属性已适当更改,但是如果我钻研了 AlertControl.ToolTip ,我发现 Visiblity DependencyProperty有绑定错误。我还尝试使用 RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type local:AlertControl}} ,但这也带来了绑定问题。我不知道该如何诊断,因为我的输出窗口也不会吐出任何绑定表达式错误。

The problem is that this doesn't seem to work. I used Snoop to spy on my xaml, and the IsAlert property is changing appropriately, but if I delve into my AlertControl.ToolTip, I see that the Visiblity DependencyProperty has a binding error. I also tried using RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:AlertControl}}, but that also gave a binding issue. I don't know how else to diagnose this because my output window is not spitting out any binding expression errors either.

推荐答案

何时它涉及到WPF ToolTip ,您必须了解它不像其他控件一样属于VisualTree。它仅在有必要创建它时才起作用,也就是鼠标悬停在控件上时。目前,WPF将创建 ToolTip ,但不会将其放置为 AlertControl 的子元素,这就是为什么 RelativeSourceMode s( TemplatedParent FindAncestor )无效。

When it comes to a WPF ToolTip, you have to understand that it is not part of the VisualTree the same way other controls are. It only comes into play when it is necessary to create it, which is when your mouse is hovering over the control. At this time, WPF will create the ToolTip, but it will not place it as a child element of the AlertControl, which is why both of the RelativeSourceModes (TemplatedParent and FindAncestor) did not work.

虽然有一个节省的宽限期,但这就是 ToolTip.PlacementTarget 属性。

There is one saving grace though, and that is the ToolTip.PlacementTarget property.

<ToolTip Visibility="{Binding Path=PlacementTarget.(local:AlertControl.IsAlert),
                              RelativeSource={RelativeSource Self},
                              Converter={...}}">
    ...
<ToolTip>

您在这里正在告诉WPF BindingExtension 绑定到名为 PlacementTarget 的属性(恰好是创建 UIElement > ToolTip ,根据您的情况选择 AlertControl ),并且您打算从 ToolTip 本身(不是 ToolTip DataContext )。除此之外,您还计划使用 IValueConverter 。此外,完整的未解析的 PropertyInfo 不仅会在 ToolTip 中查找 PlacementTarget 。 c $ c>,但还会检查从 PlacementTarget 返回的对象是否可以转换为 AlertControl 的类型。 ,然后访问其 IsAlert CLR属性。我本可以很容易地完成 Path = PlacementTarget.IsAlert 的工作,但经过反思,它还是可以的,但我想明确指出应该访问IsAlert属性。来自 AlertControl 类型。

What you are doing here is telling the WPF BindingExtension to bind to the property named PlacementTarget (which happens to be the UIElement that created the ToolTip, the AlertControl in your situation), and you are stating to locate this property from the ToolTip itself (not the DataContext of the ToolTip). Beyond that, you are also planning to use a IValueConverter. Additionally, the full unparsed PropertyInfo not only looks for the PlacementTarget on the ToolTip, but also checks to see if the object returned from PlacementTarget can be cast as a type of AlertControl, and then access its IsAlert CLR property. I could have easily have done Path=PlacementTarget.IsAlert and, with reflection, it would have worked out just fine, but I prefer to explicitly state that the IsAlert property should be accessed from a type of AlertControl.

这篇关于绑定到wpf自定义控件依赖项属性以获得工具提示吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆