我有一个GridView控件,并且我正在添加GridViewColumns。我使用的是GridViewColumn的DisplayMemberBinding属性,但现在我想使用CellTemplate。我正在装订一本字典。
以下代码适用于DisplayMemberBinding:
var column = new GridViewColumn
{
Header = current.Key,
DisplayMemberBinding = new Binding("[" + current.Key + ]")
};现在,我需要对CellTemplate执行同样的操作,但由于某些原因,我不确定为什么它不显示这些项目。
var column = new GridViewColumn
{
Header = current.Key,
CellTemplate = (DataTemplate)FindResource("GridViewTextBlockDataTemplate"),
};下面是在Window.Resources中定义的DataTemplate:
<DataTemplate x:Key="GridViewTextBlockDataTemplate" x:Name="GridViewTextBlockDataTemplate">
<TextBlock Text="{Binding Path=[Key]}"></TextBlock>
</DataTemplate>谢谢,阿扎姆
发布于 2012-11-23 05:51:31
你的解决方案是正确的,除了要获取文本块中的值,你只需要绑定关键字,而不需要在绑定数据模板时提供路径,它会自动赋值。
如果这不起作用,请尝试使用数据网格的数据模板选择器,有关数据模板选择器的详细说明,请查看这些链接:WPF DataTemplate Selector Tutorial
https://stackoverflow.com/questions/2380540
复制相似问题