c# - 如何为每个 StackPanel 项目设置单独的工具提示?

标签 c# wpf image tooltip stackpanel

我是 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/

相关文章:

c# - 组合框 ItemSsource 未与 ViewModel 中的数据绑定(bind)

wpf - 哪个重量最轻 : Canvas or Grid?

css - Bootstrap 圆形响应图像未调整到相同的圆圈大小

python - 使用命令行工具组合多个图像

c# - 需要在c#中按键和值交叉2个字典

javascript - 上传后重命名或删除客户端计算机上的文件

wpf - Caliburn.Micro - 包装模型还是直接暴露它?

c# - 带有 Swagger 和 FluentValidation 的 Web API 2 文档

c# - 获取 DNA 子串的原始顺序

javascript - 如何使用 Javascript/HTML 在 Adob​​e AIR 应用程序中显示 'Base64' 编码图像