2017-07-07 116 views
1

不同的选择颜色我有一个DataGrid:WPF有DatagridRow和DataGridCell

<DataGrid x:Name="grid" ItemsSource="{Binding DataGridCollection}" Margin="0,61,0,0" SelectionUnit="Cell"> 
      <DataGrid.CellStyle> 
       <Style TargetType="DataGridCell"> 
        <Style.Triggers> 
         <Trigger Property="IsSelected" Value="True"> 
          <Setter Property="BorderBrush" Value="Transparent" /> 
          <Setter Property="BorderThickness" Value="0"/> 
          <Setter Property="Background" Value="#FF0070CD" /> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </DataGrid.CellStyle> 
      <DataGrid.RowStyle> 
       <Style TargetType="DataGridRow"> 
        <Style.Triggers> 
         <Trigger Property="IsSelected" Value="True"> 
          <Setter Property="BorderBrush" Value="Transparent" /> 
          <Setter Property="BorderThickness" Value="0"/> 
          <Setter Property="Background" Value="#FF90CDFF" /> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </DataGrid.RowStyle> 
     </DataGrid> 

SelectionUnit被设置为 “Cell”。我想实现的是,当我选择一个单元格,它集例如蓝色到整个行和青色到选定单元格。

这是什么不工作?这是结果我得到了现在:

enter image description here

这是我们的目标:

enter image description here

回答

0

好了,试试这个对于后一点我深信,你不能这样做纯粹用XAML。这是因为您可以选择一个单元格或一行,但不能同时选择一个单元格和一行。虽然我可能在这里错了。

SelectionUnit应该还是单身,但我们只会使小区选择样式。在这里,我选择了一种绿色的颜色。

<DataGrid MouseDoubleClick="DataGrid_DoubleClick" SelectionUnit="Cell"> 
    <DataGrid.CellStyle> 
     <Style TargetType="DataGridCell"> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter Property="BorderThickness" Value="0"/> 
        <Setter Property="Background" Value="#32CD32" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </DataGrid.CellStyle> 
</DataGrid> 

现在,我们将在后面的代码中单击它时着色该行。不是一个优雅的解决方案,而是一个解决方案。

private void DataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    DependencyObject dep = (DependencyObject)e.OriginalSource; 
    DataGridCell cell = DataGridHelper.GetDataGridCell(dep); 
    DataGridRow row = DataGridHelper.GetDataGridRow(dep); 
    if (row != null) 
    { 
     if (row != lastRowSelected) 
     { 
      if (lastRowSelected != null) 
      { 
       lastRowSelected.Background = colorOfLastRowSelected; 
      } 
      lastRowSelected = row; 
      colorOfLastRowSelected = row.Background.Clone(); 
     } 
     row.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#6A5ACD")); 
    } 
} 

为此,我们需要两个私有字段:

private DataGridRow lastRowSelected; 
private Brush colorOfLastRowSelected; 

我们需要这些记住我们选择前行有什么颜色。这也适用于交替排列的颜色。

最后,我们需要一个帮手funtion得到DataGridRow:

public static DataGridRow GetDataGridRow(DependencyObject dep) 
{ 
    while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader)) 
    { 
     dep = VisualTreeHelper.GetParent(dep); 
    } 

    if (dep is DataGridColumnHeader) 
    { 
     DataGridColumnHeader columnHeader = dep as DataGridColumnHeader; 
    } 

    DataGridRow row = null; 

    if (dep is DataGridCell) 
    { 
     DataGridCell cell = dep as DataGridCell; 

     // navigate further up the tree 
     while ((dep != null) && !(dep is DataGridRow)) 
     { 
      dep = VisualTreeHelper.GetParent(dep); 
     } 

     row = dep as DataGridRow; 
    } 

    return row; 
} 

无论如何,你需要了应该得到你!祝你好运。

相关问题