Silverlight - TreeView和HierarchialDataTemplate

时间:2013-01-31 14:52:43

标签: vb.net xaml silverlight-4.0 treeview hierarchicaldatatemplate

原谅我的英语不好,我希望能够解释清楚。 我在Treview(SL4)中看不到子节点。 我有这种情况:

2个班级:

Public Class My_Root
    Private My_Cod_Prod As String
    Public Property Cod_Prod() As String
        Get
            Return My_Cod_Prod
        End Get
        Set(ByVal value As String)
            My_Cod_Prod = value
        End Set
    End Property

    Private My_Desc_Prod As String
    Public Property Desc_Prod() As String
        Get
            Return My_Desc_Prod
        End Get
        Set(ByVal value As String)
            My_Desc_Prod = value
        End Set
    End Property

    Private My_Cod_Desc_Prod As String
    Public Property Cod_Desc_Prod() As String
        Get
            Return My_Cod_Desc_Prod
        End Get
        Set(ByVal value As String)
            My_Cod_Desc_Prod = value
        End Set
    End Property

    Private My_Items As New My_Child
    Public Property Items() As My_Child
        Get
            Return My_Items
        End Get
        Set(ByVal value As My_Child)
            My_Items = value
        End Set
    End Property


End Class



Public Class My_Child
    Implements INotifyPropertyChanged
    Dim Selezionato As Boolean = False
    Private My_Cod_PosFis As String
    Public Property Cod_PosFis() As String
        Get
            Return My_Cod_PosFis
        End Get
        Set(ByVal value As String)
            My_Cod_PosFis = value
        End Set
    End Property
    Private My_Desc_PosFis As String
    Public Property Desc_PosFis() As String
        Get
            Return My_Desc_PosFis
        End Get
        Set(ByVal value As String)
            My_Desc_PosFis = value
        End Set
    End Property

    Public Property Selezione
        Get
            Return Selezionato
        End Get
        Set(ByVal value)
            Selezionato = value
            NotifyPropertyChanged("Selezione")
        End Set
    End Property

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Public Sub NotifyPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub


End Class

然后我创建一个ObservableCollection:

Public Shared nodi As New ObservableCollection(Of My_Root)()

以这种方式表达的人:

 Private Sub Compila_Class_My_Root()
   For Each Prod In Elenco_ASS_Prodotti_PosFiscali
                Dim NodoRoot As New My_Root

                    NodoRoot.Cod_Prod = Prod.PFIS_CODPROD
                    NodoRoot.Desc_Prod = Prod.PFIS_DESCOM
                    NodoRoot.Cod_Desc_Prod = Prod.PFIS_CODPROD & " - " & Prod.PFIS_DESCOM
                 Dim NodoChild As New My_Child
                    NodoChild.Cod_PosFis = Prod.PFIS_POSFIS
                    Dim desc_pos_fis As String = get_DescrizionePosFis(Prod.PFIS_POSFIS)
                    NodoChild.Desc_PosFis = desc_pos_fis
                    NodoRoot.Items = NodoChild
                nodi.Add(NodoRoot)
   Next

     MyTreeView.ItemsSource = nodi 

End Sub

这是XAML到HierarchicalDataTemplate:

       <sdk:HierarchicalDataTemplate x:Key="MyNodeRoot">
            <sdk:HierarchicalDataTemplate.ItemsSource>
                <Binding Path="My_Root" />
            </sdk:HierarchicalDataTemplate.ItemsSource>
            <TextBlock Text="{Binding Path=Cod_Desc_Prod}" />
        </sdk:HierarchicalDataTemplate>

      <sdk:TreeView Name="MyTreeView"  ItemTemplate="{StaticResource MyNodeRoot}"/>

在这里,我无法理解如何显示子节点。

2 个答案:

答案 0 :(得分:0)

我不是VB专家,但看起来你的Items属性不是数组。我认为需要声明如下。

Public Property Items() As My_Child()

然后,在Compila_Class_My_Root方法中,设置NodoRoot.Items时,也在这里使用数组:

Dim children As My_Child() = { NodoChild }
NodoRoot.Items = children

在您的XAML中,HierarchicalDataTemplate的数据上下文是My_Root实例,这就是您可以绑定到My_Root(Cod_Desc_Prod)的属性的原因。 ItemsSource也应该绑定到My_Root的属性:

<Binding Path="Items" />

或者,您可以使用较不详细的语法:

   <sdk:HierarchicalDataTemplate x:Key="MyNodeRoot" ItemsSource="{Binding Items}">
        <TextBlock Text="{Binding Cod_Desc_Prod}" />
    </sdk:HierarchicalDataTemplate>

最后,您需要指定儿童的外观。

<DataTemplate x:Key="MyChildTemplate">
    <StackPanel Orientation="Horizontal">
        <CheckBox IsChecked="{Binding Selezione, Mode=TwoWay}" />
        <TextBlock Text="{Binding Desc_PosFis}" />
        <!-- Or whatever you want here... -->
    </StackPanel>
</DataTemplate>

<sdk:HierarchicalDataTemplate x:Key="MyNodeRoot"
    ItemsSource="{Binding Items}"
    ItemTemplate="{StaticResource MyChildTemplate}"
    >
    <TextBlock Text="{Binding Cod_Desc_Prod}" />
</sdk:HierarchicalDataTemplate>

答案 1 :(得分:0)

非常感谢安德鲁,我解决了这个问题(尽管它不是一个非常漂亮的解决方案)可能对某些人有用):

我参加了课程:My_Child

    Public Property Selezione
    Get
        Return Selezionato
    End Get
    Set(ByVal value)
        Selezionato = value
        NotifyPropertyChanged("Selezione")
    End Set
    End Property

并添加到此属性:

     Private My_Cod_Prod As String
    Public Property Cod_Prod() As String
        Get
            Return My_Cod_Prod
        End Get
        Set(ByVal value As String)
            My_Cod_Prod = value
        End Set
    End Property

以这种方式表达的人:我修改了

 Private Sub Compila_Class_My_Root()
    For Each Prod In Elenco_ASS_Prodotti_PosFiscali
            Dim NodoRoot As New My_Root

                NodoRoot.Cod_Prod = Prod.PFIS_CODPROD
                NodoRoot.Desc_Prod = Prod.PFIS_DESCOM
                NodoRoot.Cod_Desc_Prod = Prod.PFIS_CODPROD & " - " & Prod.PFIS_DESCOM
             Dim NodoChild As New My_Child
                NodoChild.Cod_PosFis = Prod.PFIS_POSFIS
                Dim desc_pos_fis As String = get_DescrizionePosFis(Prod.PFIS_POSFIS)
                NodoChild.Desc_PosFis = desc_pos_fis
                NodoChild.Cod_Prod = Prod.PFIS_CODPROD  '******   ADDED   ******'
                NodoRoot.Items = NodoChild
            nodi.Add(NodoRoot)
      Next

   MyTreeView.ItemsSource = nodi 

End Sub

然后在这里我将搜索集合中的项目

    Private Sub MyTreeView_SelectedItemChanged(sender As Object, e As                    System.Windows.RoutedPropertyChangedEventArgs(Of Object)) Handles MyTreeView.SelectedItemChanged
    Dim myTreeView As TreeView = TryCast(sender, TreeView)
    Dim chkele As My_Child
    Dim chkprod As My_Root
    Dim elemento 'elemento selezionato
    Try
        chkele = myTreeView.SelectedItem

    Catch ex As Exception
        chkele = Nothing
    End Try
    If chkele Is Nothing Then
        '    ho selezionato il prodotto
        chkprod = myTreeView.SelectedItem
    End If


     Try
       elemento = (From ele In Elenco_ASS_Prodotti_PosFiscali Where ele.PFIS_CODPROD = chkele.Cod_Prod And ele.PFIS_POSFIS = chkele.Cod_PosFis).Single
    Catch ex As Exception

    End Try

    End Sub