TreeViewItem不能用datacontext选择

时间:2012-08-01 13:34:28

标签: c# wpf data-binding datacontext hierarchicaldatatemplate

当我通过DataContext设置treeviewitem的标头时,它会添加几个可点击的填充像素,然后将文本放入不可点击的位置。我会张贴一张图片;蓝色:可点击,红色:不可点击。

存储数据的类:

public class TagClass
{
    public string TagClassMagic { get; set; }
    public ITagClass RawClass { get; set; }
    public List<TagEntry> TagEntries = new List<TagEntry>();

    public IList Children
    {
        get
        {
            return new CompositeCollection()
                {
                    new CollectionContainer() { Collection = TagEntries }
                };
        }
    }

}
public class TagEntry
{
    public string TagFileName { get; set; }
    public ITagEntry RawTag { get; set; }
}

用于显示数据的XAML:

<TreeView x:Name="tvTagList" Margin="15, 40, 15, 50" ItemsSource="{Binding}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="{x:Null}" BorderBrush="{DynamicResource ExtryzeAccentBrushSecondary}" BorderThickness="2" ScrollViewer.CanContentScroll="True" Foreground="White">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type  DataBind:TagClass}" ItemsSource="{Binding Children}" >
            <TreeViewItem Header="{Binding TagClassMagic}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type DataBind:TagEntry}"  >
            <TreeViewItem Header="{Binding TagFileName}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

1 个答案:

答案 0 :(得分:2)

尝试删除这些:

<TreeViewItem Header="{Binding TagClassMagic}" />
<TreeViewItem Header="{Binding TagFileName}" />

而是相应地为TagEntry添加数据模板 - 在这些数据模板中放置简单的文本块