Selective data binding

4 posts, 0 answers
  1. Roni
    Roni avatar
    2 posts
    Member since:
    Jul 2010

    Posted 15 Aug 2010 Link to this post

    Hi, I have a TreeView and i used data binding to populate the tree (through the TreeView.ItemSource property).

    Also we are using the HirerchialDataTemplate so my tree can populate itself.

    Now i am facing a problem as i want not all the data in the data source to become treeViewItems and to be shown in the tree.

    i tried using this and it isn't working:

    void mapTreeView_ItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)
            {
                if (((MapElementItem)e.PreparedItem.Item).IsDevice)
                {
                    e.PreparedItem.Visibility = Visibility.Collapsed;
                }

            }

            void mapTreeView_ItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)
           
    {
               
    if (((MapElementItem)e.PreparedItem.Item).IsDevice)
               
    {
                    e
    .PreparedItem.Visibility = Visibility.Collapsed;
               
    }
           
    }

    so i cant even hide the nodes. i also tried changing the data template and again no success.

    any ideas on how can i accomplish a thing like that without changing my data source? or is it non nonnegotiable with the tree and the entire data source is generated.

    *if you wish more source code i can upload more but i think it is pretty clear what i am trying to do.

  2. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 18 Aug 2010 Link to this post

    Hello Roni,

    Generally there are two approaches to solve the issue:
    1. Use the Visibility property to manage if the item is visible or not.
    2. Filter the data provider to include only the items that need to be displayed.

    I would recommend the second approach. Semantics of the data context is that it contains the data that should be displayed. You can wrap your data provider (suppose it is some kind of collection) in custom class exposing the original data provider collection and additional property exposing only the required items. Of course you need to maintain the both collection when one of them change.

    If second approach is not applicable in some reason you can employ the first one.

    I attached a working sample demonstrating how to show only required elements. There are several steps that worth attention:
    1. Create view model for the tree view.
    2. Extend the view model with additional Visibility property.
    3. Bind view model Visibility to the tree view item Visibility.
    4. Implement INotifyChanged interface in order to support two way binding.
    5. Work with visibility property of the data to manage when the tree view item is visible or not.

    Sincerely yours,
    Hristo Milyakov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Roni
    Roni avatar
    2 posts
    Member since:
    Jul 2010

    Posted 19 Aug 2010 Link to this post

    Thanks Hristo for the reply, it was very helpful the problem is that we have a huge tree and we must use the IsVirtualization property and virtualize the tree nodes.

    after i virtualized your tree your solution wan't working. Sorry if i didn't mentioned it earlier but i didn't think it would affect this.

    So if my tree is virtualized what can i do to selective show the items?

    thanks in advance,
    Roni Cohen Aharon
  4. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 25 Aug 2010 Link to this post

    Hello Roni,

    Unfortunately while using virtualization you won't be able to set the visibility property (it won't take effect). We do not support the combination of using both properties. So in this case you should filter the collection used for binding the tree view.

    I hope you find a way to implement the filtration of the collection. If that solution is not applicable to your scenario I would like to ask you to elaborate on the difficulties you face.

    Regards,
    Hristo Milyakov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top