WPF Dynamic DataTemplate:GridView CellTemplate不为null,但VisualTree属性为空

问题描述:

我试图在运行时动态设置我的ListView.GridView的CellTemplate的DataTemplate。 问题是当我这样做时,没有任何反应。我检查了CellTemplate,它不是null,但其VisualTree属性为null。 有什么建议吗?WPF Dynamic DataTemplate:GridView CellTemplate不为null,但VisualTree属性为空

GridViewColumn gvc = new GridViewColumn 
    { 
     Header = col.Label ?? col.Name, 
     DisplayMemberBinding = binding       
    }; 

DataTemplate cellTemplate = FindDataTemplate(listView, col.CellTemplate); 
if (cellTemplate != null) 
    gvc.CellTemplate = cellTemplate; 

gridView.Columns.Add(gvc); 

显然,当你设置DisplayMemberBinding,则CellTemplate被忽略。所以当使用CellTemplate时,请勿设置DisplayMemberBinding

http://krishnabhargav.blogspot.com/2009/06/gridviewcolumn-celltemplate-does-not.html