Jake
Jake

Reputation: 783

Setting tooltip width and height dynamically

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

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

Upvotes: 1

Views: 12022

Answers (2)

eesh
eesh

Reputation: 1414

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

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

This will do the trick.

Upvotes: 3

Charlie
Charlie

Reputation: 15247

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?

Upvotes: 1

Related Questions