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)
Yur*_*uri 19
只需添加IsHitTestVisible="False"到DataGrid定义.
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"会禁用与行的所有交互,包括编辑.
但是,如果您要做的只是在选择时更改行的样式,请在此处查看答案.
Rhy*_*ous 10
所有这些都是轻松破解的好主意.但是,他们并没有完全按照要求做.其他答案告诉我们如何取消选择用户选择的内容或隐藏用户选择的内容的事实.
但是,我理解为什么会给出这些答案.提供真正的解决方案并不容易.
真正的解决方案是首先防止选择,这不是直截了当的,但只需几个简单的步骤即可.
答案1.您必须在Expression Blend中复制样式(或在某处找到样式的副本).2.更改单个ItemPresenter设置.在ItemPresenter上设置IsHitTestVisible ="False"就足够了.
如果您需要更多详细信息或深入了解此操作,请参阅我的博文:
正如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)
它将取消选择所有选定的行,结果将为“未选择行”。