禁用在WPF DataGrid中的选择

svi*_*ick 49 c# wpf datagrid focus selection

如何在WPFTooklit中禁用选择DataGrid?我尝试修改适用的解决方案ListView(从WPF ListView关闭选择),但这不起作用:

<tk:DataGrid>
    <tk:DataGrid.ItemContainerStyle>
        <Style TargetType="{x:Type tk:DataGridRow}">
            <Setter Property="Focusable" Value="false"/>
        </Style>
    </tk:DataGrid.ItemContainerStyle>
    <tk:DataGrid.CellStyle>
        <Style TargetType="{x:Type tk:DataGridCell}">
            <Setter Property="Focusable" Value="false"/>
        </Style>
    </tk:DataGrid.CellStyle>
</tk:DataGrid>
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex 33

干净的方式是,只是覆盖行和单元格的样式

<DataGrid.Resources>
    <ResourceDictionary>
        <Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}">
            <Setter Property="Background" Value="{x:Null}" />
            <Setter Property="BorderBrush" Value="{x:Null}" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="{x:Null}" />
                    <Setter Property="BorderBrush" Value="{x:Null}" />
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="{x:Type DataGridRow}">
            <Setter Property="Background" Value="{x:Null}" />
            <Setter Property="BorderBrush" Value="{x:Null}" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="{x:Null}" />
                    <Setter Property="BorderBrush" Value="{x:Null}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</DataGrid.Resources>
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考,不要忘记在触发器中设置`<Setter Property ="Foreground"Value ="Black"/>`否则默认选择的文本为白色. (7认同)
  • 根本禁用选择.我不得不用"透明"替换"{x:Null}"以保持选择正常.这就是Snuggles先生推荐的方式. (2认同)
  • @JiBéDoublevé我不得不这样做,但是我没有使用硬编码值,而是把它放在DataGridCell的触发器中:`<Setter Property ="Foreground"Value ="{Binding RelativeSource = {RelativeSource Self},Path = Foreground}" />`,将其设置为已有的.奇怪的是,似乎_explicitly_在列定义上设置颜色也会删除颜色更改,而不需要setter. (2认同)
  • 如果您想保留默认样式的设置(例如通过 Material Design),则可以将其添加到 `style` 标签中: `BasedOn="{StaticResource {x:Type DataGridCell}}"` 和 `BasedOn="{ StaticResource {x:Type DataGridRow}}"` ...结果仅删除了边框,而不是整个默认样式。 (2认同)

Yur*_*uri 19

只需添加IsHitTestVisible="False"DataGrid定义.

  • 这可以防止垂直滚动条响应鼠标. (27认同)
  • 满足我需求的完美解决方案,谢谢! (4认同)
  • 像魅力一样......如果你将值绑定到INPC属性,那么你可以选择哪些行是可选的:-) (3认同)
  • 除了垂直滚动条问题之外,这还会禁用子控件的鼠标单击。 (2认同)

Jos*_*hua 15

要完全禁用DataGrid中的行选择,您可以执行以下操作:

<DataGrid>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="IsHitTestVisible" Value="False"/>
        </Style>
    </DataGrid.RowStyle>
    <!--Other DataGrid items-->
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

<Setter Property="IsEnabled" Value="False"/>由于执行上述技术导致行的样式改变,因此可以认为这比设置更有利.右键单击时,它也不会禁用上下文菜单.

最后:重要的是要注意将" IsHitTestVisible " 设置为"False"会禁用与行的所有交互,包括编辑.

但是,如果您要做的只是在选择时更改行的样式,请在此处查看答案.

  • 这个答案有点救了我的命! (2认同)

Rhy*_*ous 10

所有这些都是轻松破解的好主意.但是,他们并没有完全按照要求做.其他答案告诉我们如何取消选择用户选择的内容或隐藏用户选择的内容的事实.

但是,我理解为什么会给出这些答案.提供真正的解决方案并不容易.

真正的解决方案是首先防止选择,这不是直截了当的,但只需几个简单的步骤即可.

答案1.您必须在Expression Blend中复制样式(或在某处找到样式的副本).2.更改单个ItemPresenter设置.在ItemPresenter上设置IsHitTestVisible ="False"就足够了.

如果您需要更多详细信息或深入了解此操作,请参阅我的博文:

如何禁用WPF DataGrid中的行选择?

  • 这也不太正确 - 它会阻止`DataGridCell上发生的点击,这通常是不可取的. (11认同)

Ray*_*rns 8

正如Sonic Soul 在这里指出的那样,viky的解决方案实际上并不起作用.

以下是禁用DataGrid中的选择的实际工作代码:

grid.SelectionChanged += (obj, e) => 
  Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => 
    grid.UnselectAll())); 
Run Code Online (Sandbox Code Playgroud)


小智 5

我发现这样做的唯一正确方法是禁用DataGrid Row Style上的IsHitTestVisible属性。

尽管命名,单击事件仍将注册。除非您还想禁用滚动,否则请确保不要在整个 DataGrid 上更改此属性。

一种干净的方法是通过静态资源中的新样式(根据需要复制其他 setter)

        <Style x:Key="DataGridUnselectableRowStyle" TargetType="{x:Type DataGridRow}">
            <Setter Property="IsHitTestVisible" Value="False"/>
        </Style>
Run Code Online (Sandbox Code Playgroud)

然后将其绑定到您的 DataGrid 上

        <DataGrid
            RowStyle="{StaticResource DataGridUnselectableRowStyle}" >
            <!-- Contents -->
        </DataGrid>
Run Code Online (Sandbox Code Playgroud)


vik*_*iky -11

有一个技巧可以做到这一点。您可以处理 DataGrid(例如 dgGrid)的 SelectionChanged 事件并在处理程序中写入:

dgGrid.UnselectAll();
Run Code Online (Sandbox Code Playgroud)

它将取消选择所有选定的行,结果将为“未选择行”。

  • 不起作用:这有效:http://stackoverflow.com/questions/3046988/wpf-datagrid-disable-selected-row-styles-or-row-selecting (12认同)