2009-07-22 74 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; 

這會做到這一點。