WPF树视图,具有多种类型的子节点

时间:2015-10-05 14:02:29

标签: wpf vb.net data-binding treeview

我有一个TreeView,我绑定到一个可观察的集合,其中有两个可观察的集合:

Public Class ocSpecialPolicies
    Implements INotifyPropertyChanged
    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    Protected Overridable Sub OnPropertyChanged(ByVal Propertyname As String)
        If Not Propertyname.Contains("Changed") Then
            Changed = True
        End If
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Propertyname))
    End Sub

    Private _changed As Boolean
    Public Property Changed() As Boolean
        Get
            Return _changed
        End Get
        Set(ByVal value As Boolean)
            If _changed <> value Then
                _changed = value
                OnPropertyChanged("Changed")
            End If
        End Set
    End Property

    Public Property Items As New ObservableCollection(Of ocSpecialPoliciesItem)
    Public Property Explanations As New ObservableCollection(Of ocSpecialPoliciesExplain)

    Private _RecallNum As String
    Public Property RecallNum As String
        Get
            Return _RecallNum
        End Get
        Set(value As String)
            If _RecallNum <> value Then
                _RecallNum = value
                OnPropertyChanged("RecallNum")
            End If
        End Set
    End Property
End Class

我有一半工作:

<TreeView Name="TreeView2" Margin="3" ItemsSource="{Binding ElementName=MainWindow, Path=SpecialPolicies, UpdateSourceTrigger=PropertyChanged}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Items, UpdateSourceTrigger=PropertyChanged}">
            <TextBlock Text="{Binding Path=RecallNum}" >
            </TextBlock>
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=LaborOp}" >
                    </TextBlock>
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

我能够看到RecallNum属性和Items属性,但是我无法弄清楚如何在RecallNum下将Explanations显示为额外的子节点。

理想情况下,它看起来像这样:

RecallNum*
    Items
        LaborOp
        LaborOp
        LaborOp**
    Explanations
        *ExplanationText
        **ExplanationText

我试过了HierarchicalDataTemplate in TreeView.Resources,但我只是没有做对。绑定是我一直很难缠绕的事情。

1 个答案:

答案 0 :(得分:2)

我不知道这是否会在上下文中起作用,我现在无法尝试,但是...

        <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate TargetType="{x:Type=ocSpecialPoliciesItem}">
              ...
            </DataTemplate>
            <DataTemplate TargetType="{x:Type=ocSpecialPoliciesExplain}">
              ...
            </DataTemplate>
        </HierarchicalDataTemplate.ItemTemplate>

或者沿着这些方向做什么?

编辑: 理解我们想要一个有{2}项的TreeView(抱歉,这是c#,但我相信你可以翻译)。

您可以尝试创建一个基类,从中派生ocSpecialPoliciesItem和ocSpecialPoliciesExplain,并创建基类的可观察集合。然后在DataTemplates中使用两个TreeView

class SpecialPoliciesBase {}

class ocSpecialPoliciesItem : SpecialPoliciesBase 
{ ... }

class ocSpecialPoliciesExplain : SpecialPoliciesBase
{ ... }

class ocSpecialPolicies
{
     // this is where you put ocSpecialPoliciesItem  and 
     // ocSpecialPoliciesExplain
     // I do not see any code for populating your two collections, 
     // but I suppose you must have some somewhere
   public ObersvableCollection<SpecialPoliciesBase> SPCollection
   { get ... }
}

XAML

<TreeView 
  Name="TreeView2" 
  Margin="3" 
  ItemsSource="{
    Binding ElementName=MainWindow, 
    Path=SPCollection, 
    UpdateSourceTrigger=PropertyChanged}">

  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ...>
       <TextBlock Text="{Binding Path=RecallNum}" />

       <HierarchicalDataTemplate.ItemTemplate>
           <DataTemplate TargetType={x:Type ocSpecialPoliciesItem} > 
               <!- this is where you put the template for Item !->
           </DataTemplate>
           <DataTemplate TargetType={x:Type: ocSpecialPoliciesExplain} > 
               <!- this is where you put the template for Explanations !->
           </DataTemplate>
        </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

我有类似的工作,但没有树视图。我使用一个空的抽象ViewBase类,具有几个具体的View类。我的ViewModel有一个名为CurrentView的属性,类型为ViewBase,每个具体视图都有一个DataTemplate。对于CurrentView具有的任何派生类,我的XAML选择正确的DataTemplate