This is a migrated thread and some comments may be shown as answers.

ItemsSource Binding to a Single Object

2 Answers 508 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Alvin Liu
Top achievements
Rank 1
Alvin Liu asked on 09 Jul 2009, 03:08 PM
Hi, hopefully this will be a simple question with a quick answer.

My logical data structure is a List (eg. List<TopList>) with each item in the first list also having it's own list (eg. TopList[0].MiddleList). Finally, each MiddleList has some other class (eg. TopList[0].MiddleList[0].BottomClass), and I want to display all three in the UI with a RadTreeView for expanding and drag and drop support.

My xaml is set up as:
        <HierarchicalDataTemplate x:Key="GroupListNestedContentTemplate"
            <bui:UserControlForBottomClass/> 
        </HierarchicalDataTemplate> 
         
        <HierarchicalDataTemplate x:Key="GroupListNestedTemplate" ItemsSource="{Binding BottomClass}" ItemTemplate="{StaticResource GroupListNestedContentTemplate}"
            <bui:UserControlForMiddleLevel /> 
        </HierarchicalDataTemplate> 
         
        <HierarchicalDataTemplate x:Key="GroupListTemplate" ItemsSource="{Binding MiddleList}" ItemTemplate="{StaticResource GroupListNestedTemplate}"
            <bui:UserControlForTopLevel/> 
        </HierarchicalDataTemplate> 
 
<Controls:RadTreeView x:Name="UiElement" DataContext="{Binding}" ItemsSource="{Binding}" ItemTemplate="{StaticResource GroupListTemplate}" IsDragDropEnabled="True" IsSingleExpandPath="True" IsExpandOnSingleClickEnabled="True" /> 

With codebehind setting UiElement.DataContext = TopList.

The problem is that (from all the examples I've seen) the RadTree requires my BottomClass to be in a List. Changing my BottomClass into a List will render and databind correctly. Ideally though, I don't want to do this to my logic and create a List with only one item. My question is basically: Is there a way to DataBind and display my BottomClass without putting it into a List and without using a for loop in the code behind?

As a side note I've considered turning my UI structure into a RadTree with Expanders as the contents, but this ran into display and usability issues when collapsing the expander vs closing the tree nodes.

2 Answers, 1 is accepted

Sort by
0
Miroslav
Telerik team
answered on 10 Jul 2009, 11:53 AM
Hello Alvin Liu,

Unfortunately there is no way to bind to a single item (non-enumerable), and yes - you need to put it in a collection. If you do not want to create a collection, you can create a property (or class) that will always yield just this item. This though seems like an overkill for simulating a single item in a collection.

The TreeView item were not intended to contain larger controls, but just some data templates. Of course you can possibly achieve this with a custom ControlTemplate, but them there may be an easier way to achieve what you need.

If you want to keep just a single root item, you can disable DragDrop to that item so the user will not be able to change it. Then you just need to make sure that you do not add another item.

Kind regards,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
William
Top achievements
Rank 1
answered on 05 Jul 2010, 06:41 AM
Hi,
I needed this ability too and made it work by making a converter that took the single item and converted it into a list with a single element.
Best regards,
Pete
Tags
TreeView
Asked by
Alvin Liu
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
William
Top achievements
Rank 1
Share this question
or