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

3 Answers 3

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}" />
Sign up to request clarification or add additional context in comments.

Comments

1

You can use

treeview.ItemsSource = MyList.

Here is a tutorial how to do it using MVVM.

Comments

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.

1 Comment

I found this, which is perfect to start from: stackoverflow.com/questions/6415037/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.