禁用在WPF DataGrid中select

如何禁用在WPFTooklit的DataGridselect? 我尝试修改为ListView (从WPF ListViewclosuresselect )工作的解决scheme,但不起作用:

 <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> 

这是一个窍门。 你可以处理DataGrid的SelectionChanged事件(比如说dgGrid),并在处理程序中写入:

 dgGrid.UnselectAll(); 

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

干净的方式是,只是重写行和单元格的样式

 <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> 

只需将IsHitTestVisible="False"添加到DataGrid定义。

以上所有都是很容易黑客的好主意。 但是,他们并没有按照要求去做。 其他答案告诉我们如何取消select用户select的东西,或者隐藏用户select的东西。

但是,我明白为什么会给出这些答案。 提供真正的解决scheme并不容易。

真正的解决办法是首先防止select,这不是直截了当的,但可以通过几个简单的步骤来实现。

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

如果您需要更多的细节,或者需要深入了解,请参阅我的博客文章:

如何禁用WPF DataGrid中的行select?

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

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

这可能被认为比设置<Setter Property="IsEnabled" Value="False"/>更为有利,因为执行上述技术会导致行的样式发生变化。 它也不会禁用右键单击时出现的上下文菜单。

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

但是,如果您只想在select时更改该行的样式,请在此处查看答案。

正如Sonic Soul 在这里指出的,viky的解决scheme实际上并不奏效。

这里是实际的工作代码来禁用DataGrid中的select:

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

另一个简单的方法是用IsSelected触发器将select样式更改为透明。

如果您使用替代颜色:

 <Style TargetType="{x:Type DataGrid}"> <Setter Property="RowBackground" Value="#badeee"/> <Setter Property="AlternationCount" Value="2" /> <Setter Property="AlternatingRowBackground" Value="#92cce5"/> </Style> <Style TargetType="{x:Type DataGridCell}"> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="Foreground" Value="Black"/> </Trigger> </Style.Triggers> </Style> <Style TargetType="{x:Type DataGridRow}"> <Style.Triggers> <Trigger Property="ItemsControl.AlternationIndex" Value="0"> <Setter Property="Background" Value="#badeee"></Setter> <Setter Property="BorderBrush" Value="#badeee"></Setter> </Trigger> <Trigger Property="ItemsControl.AlternationIndex" Value="1"> <Setter Property="Background" Value="#92cce5"></Setter> <Setter Property="BorderBrush" Value="#92cce5"></Setter> </Trigger> </Style.Triggers> </Style> 

如果别人面临同样的问题,他们可能会觉得有帮助。

我们有一个要求,禁用datagrid几行,但同时允许他们的箭头键导航。 这就是为什么我们不得不切换到“IsHitTestVisible”而不是控制“IsEnabled”属性。 所以我们不能采用上面的切换到“IsEnable”属性的解决scheme。

以下是我如何解决这个问题。 我为DataGridRow创build了一个新的附加属性(RowEnable)。 这个附加属性可以绑定到一个viewmodel属性来控制“虚拟”启用和禁用。 我还为DataGridCell创build了一个新的样式,我将“IsHitTestVisible”设置为基于相同viewmodel属性的false。 所以,把它看成是一个鼠标/键盘可以看到的行,但不能看到它的单元格/列。 这意味着现在我可以根据新的附加属性(RowEnabled)来设置行的样式来查看禁用/启用。 同时,我可以查看这些行几乎禁用的工具提示。

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

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

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

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

然后绑定到您的DataGrid上

  <DataGrid RowStyle="{StaticResource DataGridUnselectableRowStyle}" > <!-- Contents --> </DataGrid>