2011-08-16 51 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; 
     } 
    }