本文介绍了在用户控件中获取工具提示以显示数据绑定文本并保持打开状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件,显示一个 TextBox 以及一个小的帮助图标。

I have a user control that shows a TextBox along with a small help icon.

我的目标是要有一个 ToolTip 弹出窗口,显示一些数据绑定文本,当鼠标悬停在帮助图标上时,保持打开状态。

My goal is to have a ToolTip pop-up, show some databound text and stay open when the mouse hovers over the help icon.

所以,为此,我在用户控件中创建了一个HelpText依赖属性,允许我将帮助文本字符串绑定到用户控件。

So, to that end I have created a HelpText dependency property in the user control allowing me to bind a help text string to the user control.

所以,我的用户控件看起来像这样

So, my user control looks something like this

<UserControl Name="textField" ...>
    <StackPanel Orientation="Horizontal">
        <TextBox Text="{Binding ElementName=textField,Path=Text}"/>
        <Image Source="{StaticResource Help.Icon}">
            <Image.ToolTip>
                <ToolTip Content="{Binding ElementName=textField,Path=HelpText}"/>
            </Image.ToolTip>
        </Image>
    </StackPanel>
</UserControl>

此代码显示工具提示,除了它是空的!此外,StaysOpen属性没有任何区别,因为工具提示在几秒钟后关闭。

This code does show the tooltip, except that it is empty! Also, the StaysOpen property does not make any difference as the tooltip shuts down after a few seconds.

有趣的是,当我直接在Image控件的工具提示属性绑定文本在工具提示弹出窗口中显示,但仍然不会保持打开状态:

Funny thing is that when I set the same binding directly on the Image control's ToolTip property the bound text is shown allright in the tooltip pop-up, however it still does not stay open:

<Image Source="{StaticResource Help.Icon}" ToolTip="{Binding ElementName=textField,Path=HelpText}">

所以我的问题是:


  1. 如何绑定用户控件的HelpText依赖属性在第一个代码示例中不起作用,但在第二个代码示例中有效?_

  2. 如何我使 ToolTip 保持开放,或者如何使 ToolTip 都保持打开并显示数据绑定文本? / li>
  1. How come the binding against the user control's HelpText dependency property does not work in the first code sample but does work in the second?
  2. How do I make the ToolTip stay open or rather how do I make the ToolTip both stay open and show the databound text?

谢谢!

推荐答案

code>工具提示不是与其他XAML相同的VisualTree的一部分,所以 DataContext 不是继承自己的方式

ToolTips are not part of the same VisualTree as the rest of your XAML, so the DataContext is not inherited the way you would expect it to be.

ToolTip ={Binding SomeProperty}将自动设置ToolTip的 DataContext SomeProperty ,但是如果您构建自定义工具提示,则必须自己执行此操作。

Writing ToolTip="{Binding SomeProperty}" will automatically set the ToolTip's DataContext to SomeProperty, however if you build a custom ToolTip you must do this yourself.

<ToolTip DataContext="{Binding PlacementTarget.DataContext,
        RelativeSource={RelativeSource Self}}" ... />

这将把ToolTip的 DataContext 绑定到

This will bind the ToolTip's DataContext to the DataContext of whatever object the ToolTip is on.

为了完成你想要做的工作,你的$ DataContext c $ c>< ToolTip> 可能看起来像这样,因为 PlaceTarget 将是您的图像

To accomplish what you're trying to do, your <ToolTip> would probably look like this, since PlaceTarget would be your Image:

<!-- Could also use something like Tag if DataContext is actually used -->
<Image DataContext="{Binding ElementName=textField, Path=HelpText}"
       Source="{StaticResource Help.Icon}">
    <Image.ToolTip>
        <ToolTip Content="{Binding PlacementTarget.DataContext,
            RelativeSource={RelativeSource Self}}"/>
    </Image.ToolTip>
</Image>

至于为什么不会保持开放,我不积极,但可能是因为属性默认为5秒,这可能会覆盖 StaysOpen 属性。

As for why it won't stay open, I'm not positive but it might be because the ToolTipService.ShowDuration property defaults to 5 seconds, and that probably overwrites the StaysOpen property.

您可以尝试将其设置为更高的值,例如

You can try setting it to something higher, such as

<Image ToolTipService.ShowDuration="60000" ... />

或者您可以尝试使用 Popup 样式设计为 ToolTip 而不是。代码可能看起来像这样:

Or you can try this workaround of using a Popup styled to look like a ToolTip instead. The code would probably look something like this:

<Popup PlacementTarget="{Binding ElementName=MyImage}"
       IsOpen="{Binding IsMouseOver, ElementName=MyImage, Mode=OneWay}">
    <TextBlock Text="{Binding ElementName=textField, Path=HelpText}" />
</Popup>

这篇关于在用户控件中获取工具提示以显示数据绑定文本并保持打开状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:14
查看更多