【发布时间】:2019-11-18 11:45:43
【问题描述】:
我正在创建 WPF 应用程序,我正在使用 DataGrid。问题是在我将DataGridCell 设置为在每个单元格中都有边距和对齐之后,选择只突出显示文本后面的背景,而不是所有单元格。
类似这样的:
样式背后的代码:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="8,0,8,0"/>
</Style>
</DataGrid.CellStyle>
如何预防?有什么办法吗?\
编辑:我在干净的项目上毫无问题地复制它,所有示例代码:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid Name="grid" Margin="10">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="0.2*" Binding="{Binding first_name}"/>
<DataGridTextColumn Header="Last Name" Width="0.2*" Binding="{Binding last_name}"/>
<DataGridTextColumn Header="E-mail" Width="0.2*" Binding="{Binding email}"/>
<DataGridTextColumn Header="Phone number" Width="0.2*" Binding="{Binding phone}"/>
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="8,0,8,0"/>
</Style>
</DataGrid.CellStyle>
</DataGrid>
</Grid>
该 xaml 的类:
using System.Windows;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Client client = new Client();
public MainWindow()
{
InitializeComponent();
client = new Client()
{
first_name = "John",
last_name = "Connor",
email = "john_connor@mail.com",
phone = "123456789"
};
this.grid.DataContext = client;
this.grid.Items.Add(client);
}
}
public class Client
{
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public string phone { get; set; }
}
}
【问题讨论】:
-
可能,通过定义
RowStyle。顺便说一句,你能分享完整的 xaml 示例吗? -
我不能,但我在新的 DataGrid 上尝试了这种样式,不幸的是没有区别,所以发布所有 xaml 不会改变任何东西。
-
您的
DataGrid中还有哪些其他样式?使用您提供的CellStyle无法重现此问题。 -
@Aakanksha 我用有问题的 xaml 示例编辑了我的帖子。
-
@PavelAnikhouski 我用有问题的 xaml 示例编辑了我的帖子。