问题描述
DataGrid控件表现异常.当我单击一行时,应将其选中.单击单元格边框或行边框时出现问题.它根本什么也没做.作为用户,我想单击行并选择它而不强迫我重新单击,因为我不小心单击了单元格之间的边框.
DataGrid control in WPF is behaving weird. When I click on a row it should be selected.There is a problem when I click on cell border or row border. It simply does nothing. As a user I want to click row and select it without forcing me to re-click cause I accidentally clicked on border between cells.
是否可以通过某种方式解决此问题,所以无论我在哪里单击它都会选择行?
Is it possible to somehow fix this behavior so no matter where I click it selects row?
我发现了这种样式的问题,我将该样式应用于DataGrid的CellStyle属性:
I discovered its a matter of this style I applied to DataGrid to CellStyle property:
<Style x:Key="CustomDataGridCellStyle" TargetType="DataGridCell">
<Setter Property="Padding" Value="2" />
<Setter Property="Margin" Value="0" />
<Setter Property="Background" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridCell">
<Border x:Name="border"
BorderBrush="#CCaaccda"
BorderThickness="1"
CornerRadius="0"
Padding="4"
>
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="true">
<Setter TargetName="border" Property="Background" Value="#CC119EDA"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果我将其删除.我徘徊在风格如何与控制交互相混淆的地方.
If I remove it its working. I wander how style can mess with interactions to control.
Padding="4"
防止在单元格之间创建的空白区域无法进行命中测试.知道如何在单元格中添加未命中的阻止填充吗?
is preventing the created empty area between cells to not be able to take hit test. Any idea how to add not hittest blocking padding to cells?
解决方案很奇怪.
<Grid Background="Transparent" IsHitTestVisible="True">
<ContentPresenter Margin="4"/>
</Grid>
边距已添加到单元格内容中,并且对单击没有反应.但是我用具有IsHitTestVisible ="True"且透明的Grid包围了它. WPF太疯狂了.
Margin is added to cell content and its not reacting to click. But I surrounded it with Grid that have IsHitTestVisible="True" and is transparent. WPF is crazy weird.
推荐答案
由于您尚未发布任何xaml,也没有发布数据网格外观的图片.很难指出问题所在.
Since you hadn't posted any xaml, or a picture of how your datagrid looks.It's hard to pint point the problem.
但是,这可能是由点击测试失败引起的.鼠标命中测试通过代表该元素的彩色像素矩阵来检测该元素.
However, This might be caused by a Hit Test failing.The mouse hit test detects an element by the matrix of colored pixels which represent it.
通过将背景设置为透明",尝试为所有像素着色.透明不会影响行的外观,并且不会为点击测试着色:
Try coloring all the pixels by setting the background to Transparent. Transparent would not effect how your row looks and would color it for the Hit test :
<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="Transparent" />
</Style>
</DataGrid.RowStyle>
</DataGrid>
这篇关于将填充添加到单元格时,DataGrid行选择不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!