ItemsSource Binding to a Single Object

3 posts, 0 answers
  1. Alvin Liu
    Alvin Liu avatar
    1 posts
    Member since:
    Jul 2009

    Posted 09 Jul 2009 Link to this post

    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"
            <HierarchicalDataTemplate x:Key="GroupListNestedTemplate" ItemsSource="{Binding BottomClass}" ItemTemplate="{StaticResource GroupListNestedContentTemplate}"
                <bui:UserControlForMiddleLevel /> 
            <HierarchicalDataTemplate x:Key="GroupListTemplate" ItemsSource="{Binding MiddleList}" ItemTemplate="{StaticResource GroupListNestedTemplate}"
    <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. Miroslav
    Miroslav avatar
    922 posts

    Posted 10 Jul 2009 Link to this post

    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,
    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.
  3. William
    William avatar
    15 posts
    Member since:
    Jun 2010

    Posted 05 Jul 2010 Link to this post

    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,
Back to Top