簡體   English   中英

動態設置工具提示的寬度和高度

[英]Setting tooltip width and height dynamically

在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);`

在您的代碼中,只需將工具提示的height和width屬性設置為Double.NaN即可動態調整寬度和高度。

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

這樣就可以了。

工具提示的高度和寬度基於其內容。 所以你應該簡單地使內容符合你想要的大小。

也許您可以發布設置工具提示的代碼,以便進一步說明?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM