C# 将填充添加到单元格时,DataGrid行选择不起作用

C# 将填充添加到单元格时,DataGrid行选择不起作用,c#,wpf,datagrid,padding,C#,Wpf,Datagrid,Padding,WPF中的DataGrid控件行为怪异。当我点击一行时,它应该被选中。 单击单元格边框或行边框时出现问题。它什么也不做。作为一个用户,我希望单击行并选择它,而不强迫我重新单击,因为我无意中单击了单元格之间的边框 是否有可能以某种方式修复此行为,以便无论我在何处单击它都会选择行 [编辑] Padding="4" 我发现这是我应用于DataGrid to CellStyle属性的样式问题: <Style x:Key="CustomDataGridCellStyle" TargetType=

WPF中的DataGrid控件行为怪异。当我点击一行时,它应该被选中。 单击单元格边框或行边框时出现问题。它什么也不做。作为一个用户,我希望单击行并选择它,而不强迫我重新单击,因为我无意中单击了单元格之间的边框

是否有可能以某种方式修复此行为,以便无论我在何处单击它都会选择行


[编辑]

Padding="4"
我发现这是我应用于DataGrid to CellStyle属性的样式问题:

<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>
正在防止单元格之间创建的空白区域无法进行命中测试。知道如何在单元格中添加非命中阻塞填充吗


[编辑]

Padding="4"
这个解决方案很奇怪

<Grid Background="Transparent" IsHitTestVisible="True">
    <ContentPresenter Margin="4"/>
</Grid>


边距被添加到单元格内容中,其不会对单击作出反应。但我用网格包围了它,网格具有ishitestvisible=“True”并且是透明的。WPF非常奇怪。

因为您没有发布任何xaml,也没有发布datagrid外观的图片。 很难指出问题所在

但是,这可能是命中测试失败造成的。 鼠标点击测试通过表示元素的彩色像素矩阵来检测元素

通过将背景设置为透明,尝试为所有像素着色。透明不会影响行的外观,并会为命中测试对其进行着色:

<DataGrid>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="Transparent" />
        </Style>
    </DataGrid.RowStyle>
</DataGrid>


请添加xaml和隐藏的代码。单击WPF DataGrid行时,默认情况下应选择该行。也许代码中有错误。您需要提供您的代码。:)Padding=“4”似乎在为无法命中测试的单元格内容添加空间。我还向边框添加了ishitestvisible=“True”,它现在工作正常。我想用hittest添加这个填充。也许有人能解决这个问题。我相信我的回答能解决你的问题。行边框和实际单元格内容之间留有4个像素不可单击,这意味着它们无法捕获命中测试。我可以将其设置为0,没有问题,但我希望单元格内容的边框也有一些可单击的空间。添加没有帮助你的解决方案给了我一个想法。添加了用IshitteVisible=“True”和透明背景包围每个单元格的网格