向单元格添加填充时,DataGrid 行选择不起作用 [英] DataGrid row selection not working when padding is added to cells

查看:15
本文介绍了向单元格添加填充时,DataGrid 行选择不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF 中的 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"

阻止在单元格之间创建的空白区域无法进行命中测试.知道如何向单元格添加 not hittest 阻塞填充吗?

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 行选择不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆