代码之家  ›  专栏  ›  技术社区  ›  Jerod Houghtelling

当用户在数据网格行中单击时,不要高亮显示选定的单元格

  •  1
  • Jerod Houghtelling  · 技术社区  · 15 年前

    我有这样一个数据网格:

    <DataGrid 
        AutoGenerateColumns="True"
        GridLinesVisibility="Horizontal"
        IsReadOnly="True" 
        ItemsSource="{Binding Documents}" 
        SelectionMode="Single"                  
        SelectionUnit="FullRow"
        />
    

    Data grid with cell selected

    2 回复  |  直到 15 年前
        1
  •  0
  •   Marko    15 年前

    你会想熟悉单元格样式。我认为默认的单元格样式检查IsSelected,如果是,边框将用黑色笔刷着色。

    因为您使用的是AutoGenerateColumns,所以在代码隐藏中生成列之后,可能需要设置这些列的样式。

    我猜如果您创建一个样式,检查IsSelected并将borderbrush设置为transparent,为datagrid的列设置样式(ElementStyle+ElementEditingStyle),那么您应该被设置。我是从头开始写的,但这是我想的大致方向。

        2
  •  0
  •   Jerod Houghtelling    15 年前

    谢谢马尔科给我指出了正确的方向。下面是我如何改变我的数据网格,使它看起来不像任何单元格被选中。相反,现在似乎整行都被选中了。我选择将边框的背景设置为当前单元格的背景,这样就不必设置边框的厚度。

    <DataGrid ...>
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell" >
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter
                            Property="BorderBrush" 
                            Value="{Binding RelativeSource={RelativeSource Self}, Path=Background}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>
    
    推荐文章