Setup WPF Data Template With Class in Another Mudule

Ken Krugh 116 Reputation points
2020-05-12T03:32:47.923+00:00

Trying to bind a TreeView to a class that's in a module. I was fortunate to get this fabulous example from Peter Fleischer. In it he uses:

xmlns:local="clr-namespace:WpfApp1"

Then later:

<HierarchicalDataTemplate DataType="{x:Type local:Window63FntSetData}" ItemsSource="{Binding Fnts}">

In place of Peter's "Window63FntSetData" I need to reference a class that's in a module.

How do I do so in the XAML?

Thank you,
Ken

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,667 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-05-12T07:42:04.437+00:00

    Hi Ken,
    set reference in project to another dll and include additional namespace (from another dll) in XAML:

     xmlns:local="clr-namespace:WpfApp1"
     xmlns:externdll="clr-namespace:ExternalNamespace;assembly=ExternalAssembly"
    ...
     <HierarchicalDataTemplate DataType="{x:Type externdll:FntSetData}" ItemsSource="{Binding Fnts}"> 
    
    0 comments No comments

  2. Alex Li-MSFT 1,096 Reputation points
    2020-05-12T08:07:21.507+00:00

    Welcome to our Microsoft Q&A platform!

    You could refer to the below step to add the class in XAML.

    1. Create your model in MyVM.vb
         Public Class MyMoudle
      Public Property SetId As String
      Public Property SetName As String
      
      End Class
    2. Add the MyMoudle data for Collection
          Public Class MyVM
      
      Private col As New ObservableCollection(Of MyMoudle)
      Private cvs As New CollectionViewSource
      
      Public ReadOnly Property MyDataItems As ICollectionView
          Get
              If cvs.Source Is Nothing Then GetData()
              Return cvs.View
          End Get
      End Property
      
      Private Sub GetData()
      'add data for col
      End Sub End Class
    3. Bind the data in xaml using DataContext
      <Window.DataContext>
              <local:MyVM/>
          </Window.DataContext>
      
      1. You can bind the class in the Treeview Resource with set ItemsSource as your data collection [MyDataItems] <TreeView ItemsSource="{Binding MyDataItems}"….>
        <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:MyMoudle}" … >
        ...
        </HierarchicalDataTemplate>
        </TreeView.Resources>
        </TreeView>

    Thanks.

    0 comments No comments

  3. Ken Krugh 116 Reputation points
    2020-05-12T13:09:59.66+00:00

    Thank so much for answering guys, but I think I should have been more specific.

    The "module" I'm referring to is a .vb file in the project. Can what Peter suggested work that way?

    And I think what AlexLi suggested is the REAL "right" way to do this (is it MVVM? showing my lack of knowledge here) but I should have said that I already have an established List(of) FntSetData and FntData and am currently building the StackPalnel and adding the items to the TreeViews with .Items.Add. I'm hoping that binding the data will speed up the build of the larger list view with 10,000+ itmes when their bound.

    Currently looks like this:
    8016-capture.jpg

    I think the example that Peter supplied (linked in my original post) will get me there with some tweaking including making the "base" class (and figuring out how to add the icons to that mix) but the classes in the .vb file aren't "visible" to the XAML in the main window - if I'm even using the right terminology.

    Thanks again,
    Ken


  4. Ken Krugh 116 Reputation points
    2020-05-14T11:36:41.81+00:00

    Wow Peter! Can't thank you enough.

    Will have a look this weekend.

    All the best,
    Ken

    0 comments No comments

  5. Ken Krugh 116 Reputation points
    2020-05-18T03:33:27.24+00:00

    Got it working! My very large tree now gets built almost instantaneously! Thank you again!

    The problem was my namespaces, or more appropriately lack there of. When I said earlier that the class was in a module I was hearkening back to VB6 and meaning just another file with in the project, a .VB file. But the problem was that it WAS in a ACTUAL module with the keyword "Module" and so the XAML couldn't "see" it. I created a namespace for it and added that name space to the XMAL to fix things up.

    I found a few other things I needed this weekend with the exception of one. Before binding the data I was building the tree "manually" using a class I created inheriting form the TreeViewItem and added a couple of properties, like so:
    8451-capture.jpg

    I couldn't find a way in the XMAL to have the TreeView populate with my class rather than the usual TreeViewItem. Hoping it MIGHT help minimize the rewriting I have to do now that the data is bound.

    Thank you, yet again, for all your help.

    All the Best,
    Ken

    0 comments No comments