我是 WPF 的新手,正在使用 Stackpanel 显示图像列表。此图像列表是动态的,因此 Stackpanel 项是在运行时在代码中添加的,而不是直接在 XAML 中添加的。
当时我很好奇如何在 Stackpanel
中为每个图像显示单独的工具提示。我想我可以做如下事情:
private void mainTaskBar_PreviewMouseMove(object sender, MouseEventArgs e)
{
foreach (Image img in imgList)
if (img == e.OriginalSource)
displayCorrespondingTooltip(img);
}
但这似乎有点过分了。有什么方法可以简单地获取堆栈面板中的子项列表,并使用 SetToolTip()
简单地设置每个单独的工具提示?我觉得那样会优雅得多。
谢谢!
最佳答案
您可以定义如下方法来为各种控件分配工具提示:
public void displayCorrespondingTooltip<T>(T control, string tooltipText)
{
//For images
if (control.GetType() == typeof(Image))
{
(control as Image).ToolTip = tooltipText;
}
//For Rectangles
if (control.GetType() == typeof(Rectangle))
{
(control as Rectangle).ToolTip = tooltipText;
}
}
这辆面包车的名字如下:
displayCorrespondingTooltip<Image>(img, "This will be the image tooltip");
displayCorrespondingTooltip<Rectangle>(rectObject,"rectangle tooltip");
关于c# - 如何为每个 StackPanel 项目设置单独的工具提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36346467/