2011-08-16 6 views
1

Silverlight DataGrid에 텍스트 줄 위와 아래에 "많은"공간이 있습니다. DataGridTextColumn 의해 생성Silverlight에서 DataGridCell의 높이를 줄이는 방법은 무엇입니까?

기본 DataGridCell 인스턴스 (Silverlight Spy를 이용했다)를 마진 4TextBlock를 렌더링한다.

나는 정의 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; 
     } 
    } 
관련 문제