如何以编程方式向表单中的所有控件添加工具提示 - WPF、C#?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11628644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add tooltip to all controls in a form programmatically - WPF, C#?
提问by sony


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# 向这些控件中的每一个动态添加工具提示,以便它们可以显示以下信息:
- X and Y position
- TabIndex.
- X 和 Y 位置
- 标签索引。
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?
但这是行不通的。有什么想法吗?
回答by Rafal
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.Controlsnamespace.
请注意,在 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 thisfor how to do it.
如果您想拥有其他容器、模板以及对控件进行分组的任何内容,那么您的循环只会检查可视树中的第一级,然后您必须遍历树。请参阅此了解如何执行此操作。
回答by Aghilas Yakoub
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 ;
}
回答by Mare Infinitus
Let's suppose you do MVVM.
假设您使用 MVVM。
Then you probably have a ViewModel class. In that class, you can define a dynamic string for each control, just where you define your controls.
那么你可能有一个 ViewModel 类。在该类中,您可以为每个控件定义一个动态字符串,就在您定义控件的位置。
In the XAML, you just define the tooltip, e.g.
在 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 的更多信息,这里有一个快速教程:MSDN on MVVM
回答by Wolfgang Ziegler
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 ;
}
}

