WPF DataGrid:使用Horizo​​ntalAlignment时,未正确突出显示所选行

时间:2021-09-29 21:06:57

I have a DataGrid where one column needs to be aligned to the right. To do this I use

我有一个DataGrid,其中一列需要与右对齐。为此,我使用

<DataGridTextColumn.CellStyle>
  <Style>
    <Setter Property="FrameworkElement.HorizontalAlignment" Value="Right"/>
  </Style>
</DataGridTextColumn.CellStyle>

which works fine as shown here:

这工作正常,如下所示:

WPF DataGrid:使用Horizo​​ntalAlignment时,未正确突出显示所选行

Unfortunately the aligned cells are not properly highlighted when a row is selected. Only the data is highlighted, but the empty area to the left of the data is not, as shown here:

不幸的是,当选择行时,未正确突出显示对齐的单元格。只突出显示数据,但数据左侧的空白区域不是,如下所示:

WPF DataGrid:使用Horizo​​ntalAlignment时,未正确突出显示所选行

Additionally, the area to the left of the data is not sensitive to mouse-clicks anymore. In the example above, a click to the left of '12.34' will not select the first row (but a click to the right of 'A1' will). Altogether this makes for a bad user experience.

此外,数据左侧的区域对鼠标点击不再敏感。在上面的示例中,单击“12 .34”左侧的单击将不会选择第一行(但是“A1”右侧的单击将会出现)。总而言之,这会带来糟糕的用户体验。

So, how do I do HorizontalAlignment without breaking row-selection? I want the entire row highlighted, and I want to be able to click anywhere to select a row.

那么,如何在不破坏行选择的情况下进行Horizo​​ntalAlignment?我希望突出显示整行,我希望能够在任何地方单击以选择行。

I am using VS 2010, .NET 4, both Win XP and Win 7.

我正在使用VS 2010,.NET 4,Win XP和Win 7。

The code to reproduce my example:

重现我的例子的代码:

namespace WpfApplication2
{
  public class ListItem
  {
    public string FieldA { get; set; }
    public decimal FieldB { get; set; }
    public string FieldC { get; set; }
  }
}

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:My="clr-namespace:WpfApplication2" 
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <x:Array x:Key="List" Type="{x:Type My:ListItem}">
            <My:ListItem FieldA="A1" FieldB="12.34" FieldC="C1"/>
            <My:ListItem FieldA="A2" FieldB="1000.00" FieldC="C2"/>
            <My:ListItem FieldA="A3" FieldB="987.6" FieldC="C3"/>
        </x:Array>
    </Window.Resources>
    <Grid>
        <DataGrid ItemsSource="{StaticResource List}" AutoGenerateColumns="False" SelectionUnit="FullRow" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="ColumnA" Binding="{Binding Path=FieldA}" Width="150" />
                <DataGridTextColumn Header="ColumnB" Binding="{Binding Path=FieldB, StringFormat='#,##0.00'}" Width="150" >
                    <DataGridTextColumn.CellStyle>
                        <Style>
                            <Setter Property="FrameworkElement.HorizontalAlignment" Value="Right"/>
                        </Style>
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Header="ColumnC" Binding="{Binding Path=FieldC}" Width="*" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

1 个解决方案

#1


5  

Try DataGridTextColumn.ElementStyle and (if needed) DataGridTextColumn.EditingElementStyle

尝试DataGridTextColumn.ElementStyle和(如果需要)DataGridTextColumn.EditingElementStyle

#1


5  

Try DataGridTextColumn.ElementStyle and (if needed) DataGridTextColumn.EditingElementStyle

尝试DataGridTextColumn.ElementStyle和(如果需要)DataGridTextColumn.EditingElementStyle