2012-01-28 12 views
2

.NET 4.0では標準のWPF DataGridに関する質問が1つあります。DataGrid行の高さプロパティをプログラムで設定する

私はprogrammaticaly単純なコードを使用してデータグリッドグリッドの行の高さを設定しよう:私はグリッド行のサイズを変更しようとするまで

private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e) 
{ 
    e.Row.Height = 120;    
} 

すべてがExcelのように、マウスを使用して側のユーザーインターフェイス/標準的な方法で罰金行きます/ - グリッド行のサイズを変更することはできません。それはちょうど120であることを保ちます。ところでその内容はすべて乱れてしまいます...

Like Sinead O'Connorは言っています:私に間違っていましたか?

答えて

3

ヘッダーなどでサイズ変更された行自体の高さを設定することは意図していません。プロパティーはDataGrid.RowHeightで、適切に行うことができます。

あなたは高さを設定する必要がある場合は、選択的スタイルを作成し、項目のいくつかのプロパティにDataGridCellsPresenterの高さをバインドすることができます。

<DataGrid.Resources> 
    <Style TargetType="DataGridCellsPresenter"> 
     <Setter Property="Height" Value="{Binding RowHeight}" /> 
    </Style> 
</DataGrid.Resources> 

それとも、ビジュアルツリー(Iからプレゼンターを得ることができますないこれをお勧めします)、そこに高さを割り当ててください:

FindChildOfTypeは次のように定義できる拡張メソッドである
// In LoadingRow the presenter will not be there yet. 
e.Row.Loaded += (s, _) => 
    { 
     var cellsPresenter = e.Row.FindChildOfType<DataGridCellsPresenter>(); 
     cellsPresenter.Height = 120; 
    }; 

public static T FindChildOfType<T>(this DependencyObject dpo) where T : DependencyObject 
{ 
    int cCount = VisualTreeHelper.GetChildrenCount(dpo); 
    for (int i = 0; i < cCount; i++) 
    { 
     var child = VisualTreeHelper.GetChild(dpo, i); 
     if (child.GetType() == typeof(T)) 
     { 
      return child as T; 
     } 
     else 
     { 
      var subChild = child.FindChildOfType<T>(); 
      if (subChild != null) return subChild; 
     } 
    } 
    return null; 
} 
+0

ありがとうございます。それは問題を解決する:) – MegaMilivoje

1

これは私に役立ちます。

private void SetRowHeight(double height) 
{ 
    Style style = new Style(); 
    style.Setters.Add(new Setter(property: FrameworkElement.HeightProperty, value: height)); 
    this.RowStyle = style; 
}