【发布时间】:2013-04-04 01:07:42
【问题描述】:
我正在尝试创建一个 DataTemplate,它可以为 GridView 的所有列共享,它的列是动态创建的(通过代码隐藏)。
我想将 DataTemplate 创建为 XAML 中的资源,而不是完全在代码隐藏中,但我不知道如何让绑定正常工作。
以下是我能想到的最接近的(但不起作用):
<DataTemplate x:Key="ListViewCellTemplate">
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type GridViewColumn}}}" />
</DataTemplate>
此模板分配为每一列的CellTemplate,如下所示:
BindableDataTable table = this.DataContext as BindableDataTable;
foreach (BindableDataColumn c in table.Columns)
{
GridViewColumn col = new GridViewColumn();
col.Header = c.ColumnName;
col.CellTemplate = this.FindResource("ListViewCellTemplate") as DataTemplate;
v.Columns.Add(col);
}
【问题讨论】:
-
你能解决这个问题吗?我现在也有同样的问题:(
标签: wpf listview gridview binding