【发布时间】:2011-08-09 05:41:57
【问题描述】:
我有一个DataGrid,其中一列需要向右对齐。为此,我使用
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Right"/>
</Style>
</DataGridTextColumn.CellStyle>
效果很好,如下所示:
Unfortunately the aligned cells are not properly highlighted when a row is selected.只有数据高亮,数据左侧的空白区域没有高亮,如下图:
此外,数据左侧的区域不再对鼠标单击敏感。在上面的示例中,单击“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吗? -
FrameworkElement没有HorizontalContentAlignment属性。你能说得更具体点吗?
标签: wpf xaml datagrid alignment