【问题标题】:Change way of highlight selected rows in DataGrid with styled cells使用样式单元格更改 DataGrid 中突出显示选定行的方式
【发布时间】: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 示例编辑了我的帖子。

标签: c# wpf xaml mvvm datagrid


【解决方案1】:

看来,Margin 有问题。因此,如果可以,请尝试摆脱Margin。 如果没有,恐怕您必须覆盖默认控件模板才能更改DataGrid 的行为并有一个干净的解决方案。作为解决方法,您可以更改所选行的颜色,但如果 DataGrid 失去焦点,它将再次变得丑陋。

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Margin" Value="8,0,8,0"/>
    </Style>                
</DataGrid.CellStyle>
<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

我也会使用HorizontalContentAlignmentVerticalContentAlignment 代替HorizontalAlignmentVerticalAlignment

例如您也可以使用DataGridTemplateColumn 代替DataGridTextColumn 并将Margin 放到DataTemplate 中的控件中:

<DataGrid.Columns>
    <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding first_name}" Margin="8,0,8,0"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>                    
    </DataGridTemplateColumn>
    <!--Other columns-->
</DataGrid.Columns>

【讨论】:

  • 使用HorizontalContentAlignmentVerticalContentAlignment 并摆脱Margin 已解决的问题,谢谢。我可以使用Margin 的替代方法吗?
  • @GhostVolume 我已经扩展了答案。
猜你喜欢
  • 2012-06-06
  • 2011-05-27
  • 2020-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-20
相关资源
最近更新 更多