سؤال

forgive my English is not good, and I hope to explain well. I can not see the child nodes in Treview (SL4). I have this situation:

2 classes:

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

Then I create an ObservableCollection:

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

That people in this manner:

 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

And this is the piece of XAML to 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}"/>

And here I can not understand how can I do to display the child nodes.

هل كانت مفيدة؟

المحلول

I'm not a VB expert, but it looks like your Items property is not an array. I think it needs to be declared as follows.

Public Property Items() As My_Child()

Then, in the Compila_Class_My_Root method, when setting NodoRoot.Items, use an array here also:

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

In your XAML, the HierarchicalDataTemplate's data context is a My_Root instance, which is why you can bind to a property of My_Root (Cod_Desc_Prod). ItemsSource should also be bound to a property of My_Root:

<Binding Path="Items" />

Alternatively, you can use a less verbose syntax:

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

Lastly, you need to specify how the children should look.

<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>

نصائح أخرى

Thank you very much Andrew, I solved so (although it is not very pretty as a solution) might be useful to someone):

I took the class: My_Child

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

and added to this 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

That people in this manner: I modified

 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

then here I'm going to search for the item in the collection

    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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top