如何在TreeView中显示不同的层次结构类?

时间:2019-12-04 12:49:44

标签: wpf xaml treeview hierarchicaldatatemplate

我正在尝试为以前的仅控制台应用程序创建GUI。它用于数据评估,我正在尝试以MVVM方式进行操作,这对我作为初学者来说非常困难。 我尝试在TreeView中显示以下类:

RuleTree(由于忽略了一些属性,所以它们无关紧要):

    public class RuleTree : BaseModel, IEvaluatable<Rule>
    {
    private readonly List<Violation> _violations = new List<Violation>();
    private ChannelDataPoint _lastChannelDataPoint; 
    public TreeNode<Rule> TopRule { get; set; }
    public string Name { get; }

TreeNode:

public class TreeNode<TRule> : BaseModel where TRule : IRule 
{
    public TRule Rule { get; private set; }
    public CombinationOperator? CombinationOperator { get; private set; }
    public TreeNode<TRule> Left { get; private set; }
    public TreeNode<TRule> Right { get; private set; }

如您所见,TreeNode是分层的。只要它包含LeftRight TreeNode,我就需要能够在TreeView中查看它。

我有点迷路了。我知道解决方案是WPF中的层次数据模板。我的XAML现在看起来像这样:

            <TreeView Grid.Column="0" Margin="10" ItemsSource="{Binding Path=RuleTreeRootVM.RuleTrees}">
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type vmt:RuleTreeViewModel}" >
                        <StackPanel>
                            <TextBlock Text="{Binding Path=RuleTree.Name}" />
                            <TextBlock Text="{Binding Path=Name}" />
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
                </TreeView>

结果是这样的: Result Image

它没有任何可点击项,尽管TreeNode包含Left和Right。去这里要走什么路?

0 个答案:

没有答案
相关问题