在不使用事件的情况下悬停在控件上时在标签中显示文本 [英] Display text in a label when hovering over a control without using events

查看:20
本文介绍了在不使用事件的情况下悬停在控件上时在标签中显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您将鼠标悬停在控件上时,许多程序会在状态栏中的标签中显示一小段信息,以告诉您单击它时会发生什么,然后在鼠标离开其区域时重置文本.一个例子是 PaintShop Pro X4.

A lot of programs display a little sentence of information in a label in a status bar when you hover over an control to tell you what it will do if you click it and then it resets the text when the mouse leaves its area. One example is PaintShop Pro X4.

我想在我的应用程序中执行此操作.我当然可以通过使用鼠标输入事件更改状态栏中标签的内容轻松做到这一点,然后在鼠标离开事件上清空它,就像我在之前的一些程序中所做的那样,但这意味着我必须创建那些我想要在状态栏中显示其信息的每个控件的 2 个事件处理程序(我可以使用一个处理程序进行重置,但所有控件都使用它)但是我有大量的控件用于此.有没有更好的方法可以在不使用任何 C# 或事件处理程序的情况下做到这一点?我可以在 XAML 中做到这一点,有点像布尔值到可见性转换器,但只是将文本设置为某些东西,因为我真的不想为我什至可能不用于任何东西的控件创建大量事件处理程序(例如分组框,我将在代码中使用它们,但我想要它的状态信息)?谢谢.

I want to do this in my application. I could of course easily do it by changing the content of the label in a status bar using the mouse enter event and then empty it on the mouse leave event like I have done in some of my previous programs but that means I have to create those 2 event handlers for every control that I want to have display its information in the status bar (I could use one handler for the reset and have all controls use it though) but I have a very large number of controls for this. Is there a better way I could do this without using any C# or event handlers? Could I do it in XAML, sort of like the boolean to visibility converter but one that just sets text to something because I do not really want to create loads of event handlers for controls that I may not even use for anything (like group boxes, I will naver use them in code but I want a status information for it)? Thank you.

推荐答案

您可以使用 UIElement.IsMouseOver 属性Trigger 中执行您想要的操作.尝试这样的事情:

You can use the UIElement.IsMouseOver property in a Trigger to do what you want. Try something like this:

<TextBlock>
    <TextBlock.Style>
        <Style>
            <Setter Property="Text" Value="Normal text" />
            <Style.Triggers>
                <Trigger Property="UIElement.IsMouseOver" Value="True">
                    <Setter Property="Text" Value="Mouse over text" />
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

这篇关于在不使用事件的情况下悬停在控件上时在标签中显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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