2

我很困惑如何简单地右对齐 GridView 中的某些列,而无需为每一列编写大量标记。

我不能使用 static CellTemplate,因为同时设置时会忽略单元格模板DisplayMemberBinding(请参阅MSDN 中的备注)。如果没有DisplayMemberBinding,我将回到每列一个自定义单元格模板,因为绑定不同,这就是我想要避免的。

所以样式会很棒,就像我们可以用于标题一样:

<GridViewColumn DisplayMemberBinding="{Binding Bla}" HeaderContainerStyle="{StaticResource RightAlignStyle}" />

但是,我找不到为单元格项目设置样式的属性。

可能我错过了穿过树木的森林......

4

2 回答 2

1

马库斯,这就是我要做的。咬紧牙关,以编写 10 行代码的代价获得对对齐和任何其他不受支持的属性的一流支持。您可以遍历可视化树并查找 PART_* 的东西以进行繁重的微调。

解决方案是:

1.可对齐列类:

namespace AlignableCellsProject
{
    public class AlignableTextColumn: DataGridTextColumn
    {
        protected override System.Windows.FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            FrameworkElement element = base.GenerateElement(cell, dataItem);
            element.SetValue(FrameworkElement.HorizontalAlignmentProperty, this.HorizontalAlignment);

            return element;
        }

        protected override System.Windows.FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            FrameworkElement element = base.GenerateEditingElement(cell, dataItem);
            element.SetValue(FrameworkElement.HorizontalAlignmentProperty, this.HorizontalAlignment);

            return element;
        }

        public HorizontalAlignment HorizontalAlignment
        {
            get { return (HorizontalAlignment)this.GetValue(FrameworkElement.HorizontalAlignmentProperty); }
            set { this.SetValue(FrameworkElement.HorizontalAlignmentProperty, value); }
        }
    }
}

2.消费者的XAML:

<Window x:Class="AlignableCellsProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:AlignableCellsProject"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="g" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <local:AlignableTextColumn HorizontalAlignment="Left"
                                           Width="200" Binding="{Binding}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

3 - 消费者代码背后:

namespace AlignableCellsProject
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += 
                (o, e) => 
                {
                    this.g.ItemsSource = Enumerable.Range(1, 3);
                };
        }
    }
}

于 2011-11-14T14:04:00.007 回答
-1

我没有使用.Net 4.0,但这可以达到目的......

   <tk:DataGrid ItemsSource="{Binding}" IsReadOnly="True" AutoGenerateColumns="True">
        <tk:DataGrid.Resources>
            <Style x:Key="MyAlignedColumn" TargetType="{x:Type tk:DataGridCell}">
                <Setter Property="HorizontalAlignment" Value="Right"/>
                <Setter Property="HorizontalContentAlignment" Value="Left"/>
            </Style>
        </tk:DataGrid.Resources>
        <tk:DataGridTextColumn Header="Name"
                               CellStyle="{StaticResource MyAlignedColumn}"
                               Binding="{Binding Name, Mode=TwoWay}"/>
    </tk:DataGrid>
于 2011-11-15T05:36:02.867 回答