2011-08-16 10 views
1

Silverlight DataGridのテキスト行の上下に「たくさんの」スペースがあります。SilverlightでDataGridCellの高さを減らす方法は?

DataGridTextColumnによって生成されるデフォルトDataGridCellインスタンスが(Silverlight Spyを使用して働いて)4のマージンTextBlockをレンダリングします。

私は、カスタム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; 
     } 
    } 
関連する問題