2009-07-22 65 views
1

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

回答

1

工具尖端的高度和宽度取决于其内容。所以你应该简单地将内容制作成你想要的大小。

也许你可以发布你的代码来设置工具提示,以便进一步澄清?

+0

有代码。所以问题是图像出现在工具提示内部,但工具提示和图像之间存在间隙,我希望图像覆盖所有工具提示区域 – Jake 2009-07-23 13:31:23

3

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

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

这会做到这一点。