wpf - WPF Datagrid单元格中的边距不可点击
问题描述
我为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>
解决方案
Margin="20"
在单元格内容周围创建空白区域,该区域不可点击,因为没有填充任何内容。最简单的解决方法是添加具有透明背景的边框(请参阅相关的 QA {x:Null} 与透明画笔)
<ControlTemplate TargetType="{x:Type DataGridCell}">
<!-- Here ist the margin for every cell -->
<Border Background="Transparent">
<ContentPresenter Margin="20"/>
</Border>
</ControlTemplate>
推荐阅读
- laravel - 如何在 voyager :::not connected to model 中创建视图
- c++ - 将 (-1) 添加到 std::set 迭代器
- python - 在 debian 容器中查找 python 站点包目录?
- node.js - appInsights.setup().start(); 返回错误
- swiftui - 有没有办法退出 SpriteView 游戏场景并返回 SwiftUI homeView?
- php - 我无法在数据库 Stripe Laravel 8 上收到付款
- typescript - 连接具有相同值的键对象或如果值不同则创建新对象
- javascript - localstorage 将所有内容一起显示?
- json - Json 模式在 if 条件下添加属性
- twilio - Twilio:在前门嗡嗡作响