0

I have an object MyResult that contains a list "List MyItems".

I would like to add this to a c# WPF TreeView.

What would be the best way to do so?

Is there a step-by-step tutorial for this? Since I'm getting confused with all the MVVM Classes etc.

Thanks

Dmitry Reznik
  • 6,812
  • 2
  • 32
  • 27
Stef
  • 593
  • 3
  • 10
  • 23

3 Answers3

1

I'm assuming your objects in your list have some sort of list. if thats the caseyou should look at using the hierarchicaldatatemplate.

a simple example might be something like the following. This is from a segment I used with a radtreeview but it should work the same.

 <!-- xaml -->
 <UserControl.Resources>

            <Style x:Key="_treeViewItemStyle" TargetType="telerik:RadTreeViewItem">
                <Setter Property="Visibility" Value="{Binding IsVisible, Converter={StaticResource VisibilityConverter}}" />
            <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
        </Style>

        <UI:HierarchicalDataTemplate x:Key="_treeViewTemplate" 
                          ItemsSource="{Binding NodeItems}"
                                          >

                <TextBlock Margin="3,0,0,0" Text="{Binding Header}">
                </TextBlock>

        </UI:HierarchicalDataTemplate>

    </UserControl.Resources>


    <telerik:RadTreeView x:Name="_pageNavigator" ItemsSource="{Binding InspNavList}" ItemDoubleClick="SendFocusToPage" SelectedItem="{Binding SelectedNavItem, Mode=TwoWay}" ItemTemplate="{StaticResource _treeViewTemplate}" ItemContainerStyle="{StaticResource _treeViewItemStyle}" />
tam
  • 1,583
  • 2
  • 13
  • 25
1

You can use

treeview.ItemsSource = MyList.

Here is a tutorial how to do it using MVVM.

Raj Ranjhan
  • 3,869
  • 2
  • 19
  • 29
1

There's no best way to do things. Well, actually there is, but you need a bit more data about the system itself and the current situation (as well as some profound knowledge) to know the best way to do things. Well, putting that aside.

If you'd like to use binding, you could do the following:

a. In your page/window/usercontrol set DataContext property to point to your object (MyResult).

b. In your XAML file use following snippet to bind treeView items to that list:

  <TreeView ItemsSource={Binding MyItems}>
     ....
  </TreeView>

c. Enjoy the result.

There are several things you need to consider, though: 1. You should implement DataTemplate for your MyItems objects. Default implementation would just take ToString() result to put into the tree view. 2. If you'd like to use hierarchical data (meaning the one that has levels), you should implement HierarchicalDataTemplate and point where to get children for every node in the tree. 3. You should consider using ObservableCollection for correct binding - this way every addition/deletion of an item in the list would invoke changes in the UI.

Here are several links to get you started: first, second, third.

Dmitry Reznik
  • 6,812
  • 2
  • 32
  • 27
  • I found this, which is perfect to start from: http://stackoverflow.com/questions/6415037/populate-treeview-from-list-of-file-paths-in-wpf – Stef Apr 30 '12 at 07:29