【问题标题】:How to set a style for GridView cells如何为 GridView 单元格设置样式
【发布时间】:2011-11-14 10:47:01
【问题描述】:

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

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

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

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

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

可能我错过了树林中的森林......

【问题讨论】:

  • 嗨,您使用的是什么类型的列 - 只是 DataGridTextBoxColumns?
  • 这里有一个类似的,不知道是否有帮助:stackoverflow.com/questions/3652318/datagrid-text-alignment
  • @Dmitry,在这种情况下,它是只读的,所以我使用TextBlock。非常简单,这就是为什么我要避免为许多列自定义CellTemplates。
  • 关于您提到的其他 SO 问题:看起来这个解决方案不允许我设置对齐 per column,仅适用于整个网格。另一方面,我只是触及了 WPF 可能实现的表面,所以也许我忽略了一些明显的东西。不过非常感谢!
  • WPF ListView 没有 cell 的概念。我建议为此使用更好的 UI 控件,即 DataGrid

标签: wpf gridview .net-4.0 styles wpf-4.0


【解决方案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);
                };
        }
    }
}

【讨论】:

  • 非常感谢您的工作!有趣的方法,我会尝试一下。也许我什至可以将其扩展为根据绑定属性的类型“猜测”正确的对齐方式(例如数字 -> 右对齐,文本 -> 左对齐)。
  • 这绝对是一个有趣的调整!
【解决方案2】:

我没有使用 .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>

【讨论】:

  • +1,我用过这个。除此之外,如果我们希望特定列显示对齐,那么我们可以设置 DataGridColumn.CellStyle={StaticResource CellAlignmentStyle} 其中 CellAlignmentStyle 是分配给上述样式的键。
  • 问题是针对ListViewGridView 而不是DataGrid
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多