2011-11-30 92 views
5

我正在設計一個數據表格,但我無法弄清楚如何樣式左上角的數據網格。它是灰色的領域中這樣的畫面:樣式datagrid表格 - 左上角

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

你所需要的'RowHeaders'你DataGrid的?如果不是,我建議完全刪除它們(在''標籤上設置'HeadersVisibility ='Column''),這樣拐角單元格不會顯示。 – Rachel

+3

是的,我需要它們。 – Sulby

回答

2

從這個answer我能創造這個代碼正確設置按鈕的風格:

<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(); 
    } 
}