在FullRow选择模式下禁用DataGrid当前单元格边框

时间:2010-12-28 16:05:12

标签: wpf wpfdatagrid wpf-4.0

我在行选择模式下使用DataGrid(即SelectionUnit="FullRow")。我只是想删除当用户突出显示一行时为当前单元格放置的边框,以便进行真正的全行选择(并且没有单元格级别选择)。我不介意网格保持当前单元格的概念,我只想删除那个讨厌的当前单元格边界,可能是通过改变当前单元格的样式。最简单的方法是什么?

7 个答案:

答案 0 :(得分:97)

您可以将BorderThickness的{​​{1}}设置为0

DataGridCell

答案 1 :(得分:9)

在这里看到另一个接近的答案,但它并没有摆脱Focus矩形。这是如何消除所有边界的。

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    </Style>
</DataGrid.Resources>

另外,从技术上讲,这些单元格仍然可以获得焦点(你只是看不到它),为了使tab键前进到下一行而不是下一行单元格,我根据上面的内容定义了一个单元格样式但这也增加了以下内容......

<DataGrid.Resources>
    <Style x:Key="NoFocusDataGridCell" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
        <Setter Property="Focusable"        Value="False" />
        <Setter Property="IsTabStop"        Value="False" />
        <Setter Property="IsHitTestVisible" Value="False" />
    </Style>
</DataGrid.Resources>

...然后我将其应用于除第一列定义之外的所有内容。这样Tab键就会前进到下一行,而不是下一行。

然而回到边界。如果你想隐藏它们但仍希望它们成为间距原因的布局的一部分,请将上面的内容更改为...

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    </Style>
</DataGrid.Resources>

享受! :)

答案 2 :(得分:6)

<Style x:Key="DataGrid" TargetType="DataGrid">
    <Setter Property="CellStyle">
        <Setter.Value>
            <Style TargetType="DataGridCell">
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Foreground" Value="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}" />
                <Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}" />
            </Style>
        </Setter.Value>
    </Setter>
</Style>

答案 3 :(得分:1)

如果您不想弄乱XAML样式,可以进行此简单的修改。它的效果不如XAML样式,但是您可以尝试一下,看看它是否适合您。只需单击一下单元格就可以了,但是如果您尝试拖动单元格,这将不会在以后移除焦点(尽管我很确定您可以添加另一种情况进行检查)。

private void YourDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    YourDataGrid.Focus();
}

PS:请不要忘记将事件处理程序添加到您的DataGrid的{​​{1}}属性中。

答案 4 :(得分:0)

如果只想在单元格可编辑和选中时显示边框,则可以覆盖DataGridCell模板,并在单元格IsSelected而不是IsReadOnly时添加多重触发器。如果为列或DataGrid设置IsReadOnly = true,则不会为单元格显示边框

<ControlTemplate x:Key="MellowDataGridCellTemplate" TargetType="{x:Type DataGridCell}">
    <Grid>
        <ContentPresenter VerticalAlignment="Center" />
        <Rectangle Name="FocusVisual" Stroke="White" StrokeThickness="1" Fill="Transparent" HorizontalAlignment="Stretch" 
                           VerticalAlignment="Stretch" IsHitTestVisible="false" Opacity="0" />

    </Grid>
    <ControlTemplate.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsReadOnly" Value="False" />
                <Condition Property="IsSelected" Value="True" />
            </MultiTrigger.Conditions>
            <Setter TargetName="FocusVisual" Property="Opacity" Value="1"/>
        </MultiTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

以样式

使用模板
<Style TargetType="{x:Type DataGridCell}" x:Key="MellowGridDataGridCell">
    <Setter Property="Template" Value="{StaticResource MellowDataGridCellTemplate}" />
</Style>

并使用风格

<DataGrid CellStyle={StaticResource MellowGridDataGridCell >
    ...
</DataGrid>

答案 5 :(得分:0)

如果您正在使用xceed DataGridControl,请将NavigationBehavior设置为RowOnly

<xcdg:DataGridControl NavigationBehavior="RowOnly" SelectionMode="Single"  ....

答案 6 :(得分:0)

真正的答案是:

<!-- put it in your cell style -->
<DataTrigger Binding="{Binding SelectionUnit, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Value="FullRow">
    <Setter Property="BorderThickness" Value="0" />
</DataTrigger>

问题是仅在FullRow选择模式下禁用它,但其他答案提供了一种甚至在单元格选择模式下也完全禁用它的解决方案。

回答一个非常老的问题,但不管现在还是现在仍然使用WPF和WinForms。