2011-11-30 20 views
5

私はデータテーブルをスタイリングしていますが、データグリッドの左上のフィールドスタイルを設定する方法はわかりません。これは、この絵にグレーフィールドです:あなたはそれを行う方法をスタイルデータグリッドテーブル - 左上隅

enter image description here

知っていますか?ここで

は、これまでの私のスタイルです:

<Style TargetType="{x:Type DataGrid}"> 
    <Setter Property="Margin" Value="5" /> 
    <Setter Property="Background"> 
     <Setter.Value> 
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
       <GradientStop Color="White"/> 
       <GradientStop Color="AliceBlue" Offset="1"/> 
      </LinearGradientBrush> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="RowBackground"> 
     <Setter.Value> 
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
       <GradientStop Color="#BAF0FF"/> 
       <GradientStop Color="PowderBlue" Offset="1"/> 
      </LinearGradientBrush> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="AlternatingRowBackground"> 
     <Setter.Value> 
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
       <GradientStop Color="White"/> 
       <GradientStop Color="AliceBlue" Offset="1"/> 
      </LinearGradientBrush> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="HorizontalGridLinesBrush" Value="LightGray" /> 
    <Setter Property="VerticalGridLinesBrush" Value="LightGray" /> 
</Style> 
+1

必要ですDataGrid?そうでない場合は、コーナーセルが表示されないように完全に削除することをお勧めします( ''タグの 'HeadersVisibility =" Column "'を設定してください)。 – Rachel

+3

はい私はそれらが必要です。 – Sulby

答えて

2

私が正しくボタンのスタイルを設定し、このコードを作成することができました。このanswerから::ここ
は私の作業コードは、あなたの中に `RowHeaders`を

<DataGrid>  
    <DataGrid.Resources> 
     <Style TargetType="Button" x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}"> 
      <Setter Property="Background" Value="Black" /> 
     </Style> 
    </DataGrid.Resources> 
</DataGrid> 
1

私は不完全が、作業溶液を得ました。
VisualTreeHelperでデータグリッドの「左上隅」オブジェクトを取得できます。 これは実際にはボタンです。私はあなたが次のやり方を知っていると思います。

//Change the top left button to a CheckBox 
void StyleSelectAllButton(DependencyObject dependencyObject) 
{ 
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++) 
    { 
     var child = VisualTreeHelper.GetChild(dependencyObject, i); 
     if ((child != null) && child is Button) 
     { 
      var grid = (Grid)VisualTreeHelper.GetChild(child, 0); 

      var checkBox = new CheckBox() 
      { 
       VerticalAlignment = VerticalAlignment.Center, 
       HorizontalAlignment = HorizontalAlignment.Center, 
      }; 

      checkBox.Click += OnCheckBoxClicked; 

      grid.Children.Clear(); 
      grid.Children.Add(checkBox); 
     } 
     else 
     { 
      StyleSelectAllButton(child); 
     } 
    } 
} 

//Action when the top left check box checked and unchecked 
void OnCheckBoxClicked(object sender, RoutedEventArgs e) 
{ 
    var checkBox = sender as CheckBox; 

    if (checkBox == null) 
    { 
     return; 
    } 

    if (checkBox.IsChecked == true) 
    { 
     //Change the 'dataGrid' to your DataGrid instance name 
     dataGrid.SelectAllCells(); 
    } 
    else 
    { 
     //Change the 'dataGrid' to your DataGrid instance name 
     dataGrid.UnselectAllCells(); 
    } 
} 
関連する問題