wpf:DataGrid禁用选定的行样式 - 或行选择

时间:2010-06-15 16:16:15

标签: wpf wpfdatagrid

我看到很多关于如何为DataGrid中的Selected行设置样式的示例,例如:

How can I set the color of a selected row in DataGrid

我可以禁用所选的行样式吗?我不想要覆盖选择行更改的每一件事。只是不希望任何明显的变化。要比创建模板更简单..

或..

禁用选择行,如果这更容易..但是从浏览这个看似hacky的论坛

Disable selecting in WPF DataGrid

7 个答案:

答案 0 :(得分:72)

想出XAML摆脱选择风格..不理想,但足够接近......

<Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="Foreground" Value="Black" />
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{x:Null}" />
            <Setter Property="BorderBrush" Value="{x:Null}" />
        </Trigger>
    </Style.Triggers>
</Style>

答案 1 :(得分:20)

这对我有用:

<DataGrid>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <Trigger Property="DataGridCell.IsSelected" Value="True">
                    <Setter Property="BorderBrush">
                        <Setter.Value>
                            <SolidColorBrush Color="Transparent"/>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Foreground"
                            Value="{DynamicResource
                                   {x:Static SystemColors.ControlTextBrushKey}}"/>
                    <Setter Property="Background">
                        <Setter.Value>
                            <SolidColorBrush Color="Transparent"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.CellStyle>
    <!-- ... -->
</DataGrid>

答案 2 :(得分:17)

我找到了另一种适合我情况的方法。我为所有单元格设置了这种样式,因为我不希望用户选择任何单元格。

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="IsHitTestVisible" Value="False"/>
</Style>

答案 3 :(得分:7)

我将首先回答第二个问题:要禁用行的选择,您可以更改DataGrid的RowStyle。

<DataGrid>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="IsEnabled" Value="False"/>
        </Style>
    </DataGrid.RowStyle>
    <!--Other DataGrid items-->
</DataGrid>

但是,这会更改文本样式,因为行本身现在已“禁用”。它也没有否定用户仍然可以右键单击该行来选择它的事实。如果您真的想禁用与datagrid行的任何类型的交互,您可以执行以下操作:

<DataGrid>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="IsHitTestVisible" Value="False"/>
        </Style>
    </DataGrid.RowStyle>
    <!--Other DataGrid items-->
</DataGrid>

由于行仍处于启用状态,因此文本样式不会更改。

现在,如果您只想 更改所选行的样式但仅保留功能,则可以执行以下操作(这与@Dan Stevens的答案基本相同)。 ControlTextBrushKey是系统用于为文本项着色的画笔。请查看this answer以获取DynamicResource和StaticResource之间的解释。

<DataGrid>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <Trigger Property="DataGridCell.IsSelected" Value="True">
                    <Setter Property="BorderBrush" Value="Transparent"/>
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                    <Setter Property="Background" Value="Transparent"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.CellStyle>
    <!--Other DataGrid items-->
</DataGrid>

重要的是要注意,当选择行时,上述解决方案不会更改DataGridRowHeader的样式,如下所示(选择第一行)。

enter image description here

答案 4 :(得分:5)

这是相对简单的:

datagrid.SelectionChanged += (obj, e) =>
  Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() =>
    datagrid.UnselectAll()));

这将禁用DataGrid上的所有选择。

如果您不想完全禁用选择但只是隐藏它,则需要修改模板。

答案 5 :(得分:1)

对于像我这样的人,他们有一些不同风格的单元格,并且不想覆盖所有样式,也不想为每种风格添加触发器,这是一个不错的选择:

<DataGrid.Resources>
    <SolidColorBrush 
        x:Key="{x:Static SystemColors.HighlightBrushKey}" 
        Color="#333333"/>
    <SolidColorBrush 
        x:Key="{x:Static SystemColors.HighlightTextBrushKey}" 
        Color="Black"/>
    <SolidColorBrush 
        x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" 
        Color="Black"/>
    <SolidColorBrush 
        x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" 
        Color="Black"/>
</DataGrid.Resources>

HighlightBrushKey是带有有效选择的突出显示边框,HighlightTextBrushKey是带有有效选择的文字颜色

在我的情况下,我希望非活动选择看起来未被选中:

InactiveSelectionHighlightBrushKey是选择处于非活动状态时的边框,InactiveSelectionHighlightTextBrushKey是选择处于非活动状态时的文本

仅供参考:SystemColors是一个静态类,是System.Windows.Media命名空间的一部分。你可以检查它并无耻地覆盖你不喜欢的任何颜色!

答案 6 :(得分:1)

<Style TargetType="DataGridCell">
    <Style.Triggers>
        <Trigger Property="DataGridCell.IsSelected" Value="True">
            <Setter Property="BorderBrush" Value="Transparent"/> <!--Removes brush color change-->
            <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Foreground}"/> <!--Removes foregound change-->
            <Setter Property="Background" Value="Transparent"/>  <!--Removes backgound change-->
        </Trigger>
    </Style.Triggers>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <!--Removes dotted border when on cell selection change made by keyboard-->
</Style>

我稍微修改了 Dan StevensJoshuaTheMiller's 解决方案。在以下情况下使用此解决方案:

  • 您有不同前景色的单元格,您应该使用这种方法,不要将颜色重置为 SystemColors.ControlTextBrushKey 而是保持其不变
  • 您想禁用通过鼠标和键盘进行的单元格(和行)选择
相关问题