gpt4 book ai didi

wpf - GridViewColumn CellTemplate 后面的代码

转载 作者:行者123 更新时间:2023-12-04 16:36:29 26 4
gpt4 key购买 nike

我有一个在运行时构造的 listView,即在编译时不知道这些列。

我想将 DataTemplate 应用于单元格,使 TextAlignment 属性为 TextAlignment.Right。创建列时:

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;
}

上面的示例代码无法正常工作,因为单元格内容仍向左对齐。

最佳答案

如果使用 DisplayMemberBinding,则不会使用 CellTemplate。

您必须删除行 DisplayMemberBinding 并将绑定(bind)添加为数据模板的一部分:

private static DataTemplate getDataTemplate(int count)
{
DataTemplate template = new DataTemplate();
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(TextBlock));
factory.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Right);
factory.SetBinding(TextBlock.TextProperty, new Binding(string.Format("[{0}]", count)));
template.VisualTree = factory;

return template;
}

关于wpf - GridViewColumn CellTemplate 后面的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6349739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com