简体   繁体   English

如何以编程方式向所有控件添加工具提示-WPF,C#?

[英]How to add tooltip to all controls in a form programmatically - WPF, C#?

在此处输入图片说明

I have a WPF Form with different types of control like textboxes, textblocks, combobox, buttons etc. I need to add tooltips to each of these controls dynamically using C#, so that they can display the following information: 我有一个WPF表单,其中包含不同类型的控件,例如文本框,文本块,组合框,按钮等。我需要使用C#为这些控件中的每一个动态添加工具提示,以便它们可以显示以下信息:

  1. X and Y position X和Y位置
  2. TabIndex. TabIndex。

I do the code as below for each control as below (code for textbox for now): 我为以下每个控件执行以下代码(目前为文本框代码):

 foreach (Control ctrl in grd.Children)
        {
            if (ctrl.GetType().ToString() == "System.Controls.TextBox")
            {
                tbox = ctrl as TextBox;
                Point p = Mouse.GetPosition(tbox);
                tbox.ToolTip =p.X + " " + p.Y + " \n " + tbox.TabIndex ;
            }
        }

But this is not working. 但这是行不通的。 Any thoughts? 有什么想法吗?

First of all your type checking is just plain evil. 首先,类型检查只是普通的邪恶。

 if (ctrl.GetType().ToString() == "System.Controls.TextBox")

Change it to 更改为

 if (ctrl is TextBox)

or even better 甚至更好

var textbox = ctrl as TextBox;
if(textbox != null)

Note that in wpf TextBox is in System.Windows.Controls namespace. 请注意,在wpf中,TextBox位于System.Windows.Controls命名空间中。

Your loop will only check first level in Visual Tree if you want to have other containers, templates anything that groups controls then you have to traverse the tree. 如果您要使用其他容器,将控件分组的任何内容作为模板,则循环将仅在可视树中检查第一级,然后您必须遍历该树。 See this for how to do it. 看到这个如何做。

try with this code

var controls = grd.Children.OfType<TextBox>();

foreach(var control in controls)
{
   Point point = Mouse.GetPosition(control);
   control.ToolTip = point.X + " " + point.Y + " \n " + control.TabIndex ;
}

Let's suppose you do MVVM. 假设您执行MVVM。

Then you probably have a ViewModel class. 然后,您可能有一个ViewModel类。 In that class, you can define a dynamic string for each control, just where you define your controls. 在该类中,您可以在定义控件的位置为每个控件定义一个动态字符串。

In the XAML, you just define the tooltip, eg 在XAML中,您只需定义工具提示,例如

<Button Content="Submit">
    <Button.ToolTip>
        <ToolTip>
            <StackPanel>
                <TextBlock FontWeight="Bold">Submit Request</TextBlock>
                <TextBlock>Submits the request to the server.</TextBlock>
            </StackPanel>
        </ToolTip>
    </Button.ToolTip>
</Button>

The Textblock text can be delivered in your ViewModel and Binding will get them to your View. Textblock文本可以在您的ViewModel中传递,并且Binding会将它们带到您的View。

I suppose that your ViewModel also knowns on which Tab your controls are. 我想您的ViewModel也知道控件在哪个Tab上。

For more information on Tooltips, there is the WPFTutorials site WPF Tutorial on Tooltip Controls 有关工具提示的更多信息,请访问WPFTutorials网站上的“工具提示控件的WPF教程”。

For more information on MVVM, there is a quick tutorial here: MSDN on MVVM 有关MVVM的更多信息,请参见此处的快速教程: MVVM上的MSDN。

Your type checking is wrong (namespace!). 您的类型检查错误(命名空间!)。 Try this: 尝试这个:

foreach (var ctrl in grd.Children)
        {
            if (ctrl is TextBox)
            {
                tbox = ctrl as TextBox;
                Point p = Mouse.GetPosition(tbox);
                tbox.ToolTip =p.X + " " + p.Y + " \n " + tbox.TabIndex ;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM