GridViewColumn CellTemplate code的背后 [英] GridViewColumn CellTemplate Code Behind

查看:368
本文介绍了GridViewColumn CellTemplate code的背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,我在运行时构造,即列在编译时称。

I have a listView that I construct at run-time, i.e. the columns are not known at compile-time.

我想一个DataTemplate适用于细胞使得TextAlignment属性是TextAlignment.Right。在创建列:

I would like to apply a DataTemplate to the cells such that the TextAlignment property is TextAlignment.Right. When creating the columns:

foreach (var col in dataMatrix.Columns)
{
    gridView.Columns.Add(
        new GridViewColumn
        {
            Header = col.Name,
            DisplayMemberBinding = new Binding(string.Format("[{0}]", count)),
            CellTemplate = getDataTemplate(count),
        });
    count++;
}

private static DataTemplate getDataTemplate(int count)
{
    DataTemplate template = new DataTemplate();
    FrameworkElementFactory factory = new FrameworkElementFactory(typeof(TextBlock));
    factory.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Right);
    template.VisualTree = factory;

    return template;
}

样品code以上不正常的细胞内容物工作仍在靠左对齐。

The sample code above does not work properly as the cell contents is still aligned to the left.

推荐答案

当你不使用你的DataTemplate你可以只在XAML中创建的DataTemplate计数属性,那么你知道什么属性,你在文本框设置会应用。个人而言,我会用一个DataGrid,并将其设置为只读。它为您提供了更多的灵活性创造specififc类型的动态列。

As you are not using the count property in your DataTemplate you could just create the DataTemplate in xaml, then you know that whatever properties you set on the TextBox will be applied. Personally I would use a Datagrid and set it as read only. It gives you more flexibility for creating dynamic columns of specififc types.

这篇关于GridViewColumn CellTemplate code的背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆