WPF Datagrid单元中的页边距不可单击

问题描述 投票:0回答:1

我为WPF DataGrid创建了自定义样式。

一件事是在每个单元格上增加边距。工作正常,但单击页边空白时无法选择一行。我不知道该如何解决。

<DataGrid ItemsSource="{Binding Items}">
   <DataGrid.CellStyle>
       <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <!-- Here is the margin for every cell -->
                        <ContentPresenter Margin="20"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
   </DataGrid.CellStyle>

   <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Setter Property="BorderThickness" Value="0 1 0 1"/>
            <Setter Property="BorderBrush" Value="{x:Null}"/>
            <Style.Triggers>
                <!-- mouseover works fine, even for the margin -->
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="BorderBrush" Value="Orange"/>
                </Trigger>
                <!-- this gets not set when you click on the margin in the cell, so you think when the row highlightes you also can select it, but you cant -->
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
            </Style.Triggers>
         </Style>
   </DataGrid.RowStyle>           
</DataGrid>
wpf xaml datagrid styles margin
1个回答
2
投票

Margin="20"在单元格内容周围创建空白区域,该区域不可单击,因为其中没有任何内容。最简单的解决方法是添加具有透明背景的Border(请参阅相关的QA {x:Null} vs. Transparent brush

<ControlTemplate TargetType="{x:Type DataGridCell}">
    <!-- Here ist the margin for every cell -->
    <Border Background="Transparent">
        <ContentPresenter Margin="20"/>
    </Border>
</ControlTemplate>
© www.soinside.com 2019 - 2024. All rights reserved.