C# 动态设置工具提示宽度和高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1168646/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:53:19  来源:igfitidea点击:

Setting tooltip width and height dynamically

c#wpftooltip

提问by Jake

In WPF (C#) is there a way to set the tooltip's height and width dynamically (meaning in code). Thanks for the help.

在 WPF (C#) 中有一种方法可以动态设置工具提示的高度和宽度(在代码中的意思)。谢谢您的帮助。

System.Windows.Controls.Image td = new System.Windows.Controls.Image();

BitmapImage myBitmapImage = new BitmapImage();
            myBitmapImage.BeginInit();
            myBitmapImage.UriSource = new Uri(Directory.GetCurrentDirectory() + "/Homepage.jpg");
            td.Width = 530;
            td.Height = 392;
            //myBitmapImage.DecodePixelWidth = 430;
            //myBitmapImage.DecodePixelHeight = 292;
            myBitmapImage.EndInit();
            td.Source = myBitmapImage;

            TextBlock textBlock = new TextBlock();
            BrushConverter conv = new BrushConverter();
            string strColor1 = bannerColor.SelectedItem.ToString();
            strColor1 = strColor1.Substring(strColor1.IndexOf(' ') + 1);
            SolidColorBrush col = conv.ConvertFromString(strColor1) as SolidColorBrush;

            textBlock.Foreground = col;
            textBlock.FontWeight = FontWeights.Bold;
            textBlock.FontSize = 18;
            textBlock.FontFamily = new System.Windows.Media.FontFamily("Tahoma");
            textBlock.Width = 100;
            textBlock.Height = 20;
            textBlock.Text = "BACKUP";
            textBlock.Margin = new Thickness(5, 5, 425, 367);

            Grid toolTipPanel = new Grid();
            toolTipPanel.Width = 530;
            toolTipPanel.Height = 392;
            toolTipPanel.Children.Add(td);
            toolTipPanel.Children.Add(textBlock);

            ToolTipService.SetToolTip(image1, toolTipPanel);
            ToolTipService.SetShowDuration(image1, 999999999);`

采纳答案by Charlie

A tool tip's height and width are based on its content. So you should simply make the content the size you want it to be.

工具提示的高度和宽度基于其内容。因此,您应该简单地将内容设置为您想要的大小。

Perhaps you could post your code that sets the tool tip, for further clarification?

也许您可以发布设置工具提示的代码,以便进一步说明?

回答by eesh

In your code just set the tooltip's height and width property to Double.NaN to have the width and height adjust dynamically.

在您的代码中,只需将工具提示的高度和宽度属性设置为 Double.NaN 即可动态调整宽度和高度。

_toolTip.Width = Double.NaN;
_toolTip.Height = Double.NaN;

This will do the trick.

这将解决问题。