【问题标题】:Center a WPF ListView Column in "code behind"在“代码隐藏”中将 WPF ListView 列居中
【发布时间】:2021-02-28 22:41:16
【问题描述】:

我试图在代码隐藏中的 ListView 中居中列。由于列是在运行时动态创建的,因此在 XAML 中没有执行此操作的选项。

这里是我创建的助手。设置字体颜色等其他属性效果很好 - 有什么想法吗?

private void ListViewHelper(GridView gridView, ListView listView, MatrixId assignment, string key, IValueConverter converter)
{
   // new column
   GridViewColumn gridViewColumn = new GridViewColumn();

   // header text and formating
   gridViewColumn.Header = new TextBlock { Text = assignment.Id.ToString(), TextAlignment = TextAlignment.Center, Padding = new Thickness(7, 0, 0, 0), Width = 50 };

   // databinding & converter
   FrameworkElementFactory frameworkElementFactory = new FrameworkElementFactory(typeof(TextBlock));
   DataTemplate dataTemplate = new DataTemplate();
   dataTemplate.VisualTree = frameworkElementFactory;
   gridViewColumn.CellTemplate = dataTemplate;
   Binding binding = new Binding(assignment.Id.ToString() + key);
   binding.Converter = converter;

   // *** this does not work ***             
   frameworkElementFactory.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Right);
   frameworkElementFactory.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Right);

   frameworkElementFactory.SetValue(TextBlock.ForegroundProperty, new SolidColorBrush(Color.FromRgb(100, 100, 100)));
   frameworkElementFactory.SetBinding(TextBlock.TextProperty, binding);

   // add column
   gridView.Columns.Add(gridViewColumn);
}

【问题讨论】:

    标签: c# wpf listview gridview code-behind


    【解决方案1】:

    如果您想在ListView 中将GridView 中的内容居中,您可以通过为ListViewItem 创建项目容器样式并将HorizontalContentAlignment 设置为Center 来实现。

    var itemContainerStyle = new Style(typeof(ListViewItem));
    var horizontalContentAlignmentSetter = new Setter(HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
    itemContainerStyle.Setters.Add(horizontalContentAlignmentSetter);
    
    listView.ItemContainerStyle = itemContainerStyle;
    

    这里,listView 是您的列表视图实例。对齐将应用于所有列。如果需要单独对齐列,请将项目容器模板中的HorizontalContentAlignment 设置为Stretch

    var itemContainerStyle = new Style(typeof(ListViewItem));
    var horizontalContentAlignmentSetter = new Setter(HorizontalContentAlignmentProperty, HorizontalAlignment.Stretch);
    itemContainerStyle.Setters.Add(horizontalContentAlignmentSetter);
    
    listView.ItemContainerStyle = itemContainerStyle;
    

    然后将CellTemplate内控件的HorizontalAlignment设置为您需要的。

    private void ListViewHelper(GridView gridView, ListView listView, MatrixId assignment, string key, IValueConverter converter)
    {
       // ...your code.
    
       // Set the alignment to "Left", "Center", "Right" or "Stretch"
       frameworkElementFactory.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Right);
    
       // ...your code.
    }
    

    【讨论】:

    • 非常感谢。实际上,我之前检查了第一个选项,但正如您提到的那样,它将所有列居中。如何在代码中将“项目容器模板”设置为 strech?我刚刚在 XAML 中找到了示例...
    • @Norbert 我已经为各个列对齐添加了代码。
    猜你喜欢
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 2021-12-18
    相关资源
    最近更新 更多