WPF TreeView如何指定与Child节点源

时间:2015-06-09 18:15:11

标签: c# wpf xaml treeview

我有一个Rule类列表,需要绑定到Treeview,但Treeview节点的层次结构应该是这样的

如果
--IF2
--ElseIF
---- IF3
--Else
---- IF4

这是规则类

public class Rule
{
    private List<RuleTag> elseIf = new List<RuleTag>();

    public RuleTag IF { get; set; }
    public RuleTag Else { get; set; }

    public List<RuleTag> ElseIf
    {
        get { return elseIf; }
    }

    public Rule() { }
}

RuleTag定义如下

public class RuleTag
{
    private List<Rule> children = new List<Rule>();

    public List<Rule> Children
    {
        get { return children; }
        set { children = value; }
    }

    public RuleTag() { }
}

在上面的例子中,IF2是成员IF.Children,ElseIF是Rule.ElseIF [0](如果数组中有多个ElseIf都需要显示),IF3是Rule.ElseIf [0]的成员] .Children和IF4是Rule.Else.Children

的成员

我已尝试使用HierarchicalDataTemplate,但我只能绑定到IF的子级,无法将Else和ElseIF显示为子级

这是我的XAML

<TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type TangoRules:Rule}" ItemsSource="{Binding IF.Children}">
        <TextBlock Text="IF" />
    </HierarchicalDataTemplate>
</TreeView.Resources>

2 个答案:

答案 0 :(得分:0)

尝试使用两个级别的Hierarchical Datatemplate:

<HierarchicalDataTemplate ItemsSource="{Binding RuleCollection}">
            <TextBlock Text="{Binding IF}" />                       
            <HierarchicalDataTemplate.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}" />
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>                   
                </HierarchicalDataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>

        </HierarchicalDataTemplate>

答案 1 :(得分:0)

我使用CompositeCollection通过修改Rule类来解决它,如下所示

public class Rule : INotifyPropertyChanged
{
    private ObservableCollection<RuleTag> elseIf = new ObservableCollection<RuleTag>();
    private RuleTag _else;
    private CompositeCollection children = new CompositeCollection();

    public RuleTag IF { get; set; }
    public RuleTag Else
    {
        get { return _else; }
        set
        {
            _else = value;
            OnPropertyChanged("Children");
        }
    }

    public ObservableCollection<RuleTag> ElseIf
    {
        get { return elseIf; }
    }

    public CompositeCollection Children
    {
        get
        {
            children.Clear();
            children.Add(new CollectionContainer() { Collection = IF.Children });
            children.Add(new CollectionContainer() { Collection = ElseIf });
            if (Else != null)
                children.Add(Else);
            return children;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

请注意,我现在还实现了INotifyPropertyChanged接口,因为类中的else部分不是集合。现在当Rule类被实例化时,如果Else为空,则它没有被添加到CompositeCollection,但是如果稍后设置它,我会通知CompositeCollection的所有观察者这个属性被更改。除此之外,我已经制作了所有列表,例如IF.Childre,List observablecollection