2011-08-16 55 views
1

Silverlight DataGrid中的文本行上下有「很多」空間。由DataGridTextColumn產生如何減少Silverlight中DataGridCell的高度?

默認DataGridCell實例呈現一個TextBlock緣4(摸索出使用Silverlight Spy)。

我試圖創建一個自定義模板DataGridCell並設置保證金填充值爲零那裏,但非此非彼設置ContentTemplate改變任何東西。

你知道如何將某個DataGridCell的高度降低到0旁邊的值嗎?

在此先感謝!

回答

1

我只是找到了答案由我自己:

問題出在哪裏產生的被放置在每個單元內的TextBlock中的DataGridTextColumn類的一部分:

protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) 
    { 
     TextBlock block = new TextBlock { 
      Margin = new Thickness(4.0), 
      VerticalAlignment = VerticalAlignment.Center 
     }; 
     if (DependencyProperty.UnsetValue != base.ReadLocalValue(FontFamilyProperty)) 
     { 
      block.FontFamily = this.FontFamily; 
     } 
     if (this._fontSize.HasValue) 
     { 
      block.FontSize = this._fontSize.Value; 
     } 
     if (this._fontStyle.HasValue) 
     { 
      block.FontStyle = this._fontStyle.Value; 
     } 
     if (this._fontWeight.HasValue) 
     { 
      block.FontWeight = this._fontWeight.Value; 
     } 
     if (this._foreground != null) 
     { 
      block.Foreground = this._foreground; 
     } 
     if ((this.Binding != null) || !DesignerProperties.IsInDesignTool) 
     { 
      block.SetBinding(TextBlock.TextProperty, this.Binding); 
     } 
     return block; 
    } 

正如你所看到的保證金靜態設置爲4.0。爲了解決這個問題,我創建了一個包裝類,派生自DataGridTextColumn

public class DataGridCustomTextColumn : DataGridTextColumn 
    { 
     protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) 
     { 
      //Get the parent TextBlock 
      TextBlock block = (TextBlock)base.GenerateElement(cell, dataItem); 

      if (ElementStyle != null) //if an element style is given 
      { 
       //Apply each setter of the style to the generated block 
       ElementStyle.Setters.OfType<System.Windows.Setter>() 
        .ForEach((setter) => block.SetValue(setter.Property, setter.Value)); 
      } 
      //Return styled block 
      return (FrameworkElement)objBlock; 
     } 
    }