2013-03-18 39 views
1

我目前正在建立一个使用DataGrid持有表的WPF窗口。绑定和更新工作正常,我对样式也很鄙视,但是当涉及到选择时,我遇到了麻烦。这是前提条件:在选择时更改整个数据网格行的背景颜色

  • 表是只读
  • 全行选择

这是我的表的源代码(是的,我知道我没有设置选择颜色3次,一次对于DataGrid中,一旦该行,一次电池。我想也许那些人会帮助,但它并非如此。)

<DataGrid x:Name="dgv" SelectionMode="Single" SelectionUnit="FullRow" AutoGenerateColumns="False" Grid.Column="0" Grid.RowSpan="3" Margin="8" RowHeight="32" GridLinesVisibility="Horizontal" HeadersVisibility="Column" HorizontalScrollBarVisibility="Hidden" 
       CanUserAddRows="False" 
       CanUserDeleteRows="False" 
       CanUserReorderColumns="False" 
       CanUserResizeColumns="False" 
       CanUserResizeRows="False" 
       CanUserSortColumns="True" 
       IsReadOnly="True" 
       LoadingRow="dgv_LoadingRow" 
       > 
     <DataGrid.CellStyle> 
      <Style TargetType="DataGridCell"> 
       <Style.Resources> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"></SolidColorBrush> 
       </Style.Resources> 
       <Setter Property="VerticalAlignment" Value="center"></Setter> 
       <Setter Property="Padding" Value="4"></Setter> 
       <Setter Property="Margin" Value="4"></Setter> 
      </Style> 
     </DataGrid.CellStyle> 
     <DataGrid.RowStyle> 
      <Style TargetType="DataGridRow"> 
       <Style.Resources> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"></SolidColorBrush> 
       </Style.Resources> 
      </Style> 
     </DataGrid.RowStyle> 
     <DataGrid.Style> 
      <Style TargetType="DataGrid"> 
       <Style.Resources> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"></SolidColorBrush> 
       </Style.Resources> 
      </Style> 
     </DataGrid.Style> 

然后它继续列 - 和RowDefinitions ...

我遇到了以下问题:

  • 只有细胞都被选择,而不是整条生产线。单元格中的边距使其看起来很怪异(请参阅屏幕截图)
  • 当单击单元格的边距(屏幕截图中未以红色呈现的区域)时,行不会选中 - 使选择行相当直观...
  • ,我一下就选择该行仍然得到hihglighted细胞(注意黑色边框“彼得·穆勒”选择行)

这里的结果的截图:

enter image description here

+0

嗯,我想因为没有人提出任何不同的东西,我将不得不使用编程方法并对selectionChanged事件做出反应...... – Eisenhorn 2013-03-18 18:05:02

回答

1

如果要删除单元格定义中的边距,该怎么办?这就是说占用了额外空间的单元格和没有覆盖该空间的红色单元格。如果你删除了保证金,你有没有得到你想要的。我认为真正的答案可能在DataGrid.RowBackground [Property] [1]。

属性值 类型:System.Windows.Media.Brush 绘制行背景的画笔。注册的默认值为空。有关可以影响值的更多信息,请参阅DependencyProperty。

您可以在IsSelected状态下使用触发器来设置颜色。默认情况下,DataGrid的整行被选中。

<DataGrid Name="dataGrid1" Margin="12,12,0,0"> 
    <DataGrid.RowStyle> 
     <Style TargetType="DataGridRow"> 
      <Setter Property="Background" Value="LightBlue" /> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter Property="Background" Value="Red"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </DataGrid.RowStyle> 
</DataGrid> 
+0

这不起作用,因为DataGridRow的控件模板更改模板内部的边框的背景,而不是DataGridRow本身的背景... [http://blogs.msdn.com/b/wpfsdk /archive/2007/08/31/specifying-the-selection-color-content-alignment-and-background-color-for-items-in-a-listbox.aspx] – Eisenhorn 2013-03-18 17:16:41

+0

您的链接无效。它显示“未找到组”。 – Harrison 2013-03-18 18:10:59

+0

多数民众赞成在奇怪的...我现在得到相同的结果。尽管如此,你的方法不起作用,不幸的是,它甚至不会将“选择”颜色或背景颜色切换。基本上就好像这段代码由于某种原因没有被解释... – Eisenhorn 2013-03-18 18:30:35

0

为了改变默认背景行选择。你需要 1)编辑datagridrow风格&模板(即http://msdn.microsoft.com/en-us/library/cc278066%28v=vs.95%29.aspx) 2)处理selectionchanged事件和改变背景的行。 3)或DataGrid行加载的事件获取childrenoftype矩形等于“BackgroundRectangle”并设置你想要的颜色 - 所以使用它会影响到数据网格中的所有行,它与1相似,但在代码后面执行此操作。

希望这给你一些想法。

相关问题