WPF数据表格:使用HorizontalAlignment时,选定的行未正确突出显示

3
我有一个 DataGrid,其中一列需要右对齐。为了做到这一点,我使用:
<DataGridTextColumn.CellStyle>
  <Style>
    <Setter Property="FrameworkElement.HorizontalAlignment" Value="Right"/>
  </Style>
</DataGridTextColumn.CellStyle>

这里展示的代码运行良好:

enter image description here

很遗憾,当选中一行时,对齐的单元格没有被正确地突出显示。只有数据被突出显示,但是数据左侧的空白区域没有被突出显示,如下所示:

enter image description here

此外,数据左侧的区域不再对鼠标点击敏感。在上面的示例中,单击“12.34”左侧将不会选择第一行(但单击“A1”右侧将会)。所有这些都导致了糟糕的用户体验。
那么,如何在不破坏行选择的情况下进行HorizontalAlignment?我想要整个行被突出显示,并且我想能够单击任何位置来选择一行。
我正在使用VS 2010、.NET 4,以及Win XP和Win 7。
复制示例的代码:
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>

你尝试过使用 HorizontalContentAlignment 吗? - Kent Boogaart
FrameworkElement没有HorizontalContentAlignment属性。你可以更明确一些吗? - TomBot
1个回答

6
尝试使用 DataGridTextColumn.ElementStyle 和(如有必要)DataGridTextColumn.EditingElementStyle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接