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

Getting checked items using MVVM

4 Answers 230 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Binu Abraham
Top achievements
Rank 1
Binu Abraham asked on 25 Nov 2009, 08:32 AM
Hi
I'm using a RadTreeView with checkboxes. I'm binding the ItemsSource property of the radtreeview to a collection of my businessobject. I need to retrieve the checked items (businesss objects) on my view model class. I tried to bind the CheckedItems property to an ObservableCollection on my viewmodel. but it gives me a compile error because the set accessor is not accessible.
Is there another way of doing this other than hooking it to the checked event from the code behind?

4 Answers, 1 is accepted

Sort by
0
Kiril Stanoev
Telerik team
answered on 25 Nov 2009, 09:23 AM
Hi Binu,

I have prepared a sample project that demonstrates a possible solution to your scenario.
In my business object, I have 3 properties (IsChecked, IsExpanded and IsSelected) that are used to control the 3 corresponding properties of RadTreeViewItem - CheckState, IsExpanded and IsSelected.



The way this is achieved is via style bindings:

<telerikNavigation:RadTreeView x:Name="treeView1" IsOptionElementsEnabled="True"
                SelectionMode="Multiple" ItemsOptionListType="CheckList"
                ItemsSource="{StaticResource FamiliesItemsSource}"
                ItemTemplate="{StaticResource FamilyTemplate}">
    <telerikNavigation:RadTreeView.ItemContainerStyle>
        <Style TargetType="{x:Type telerikNavigation:RadTreeViewItem}">
            <Setter Property="CheckState" Value="{Binding IsChecked, Mode=TwoWay, Converter={StaticResource CheckStateConverter}}" />
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
        </Style>
    </telerikNavigation:RadTreeView.ItemContainerStyle>
</telerikNavigation:RadTreeView>

Finally, go can go through your collection of business items and check each item if it is checked or not. If it is checked, you can add it to a second collection and give this collection to another ItemsControl(ListBox for example).
Once again, this is one possible solution. It depends on the size of your collection of business items. In my sample project I use nested loops to find the checked items, which might not be suitable for you.
Have a look at the attached sample project for further reference and let me know if you have additional questions.

All the best,
Kiril Stanoev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Binu Abraham
Top achievements
Rank 1
answered on 25 Nov 2009, 11:55 AM
Hi Kiril
Thanks for the reply. I was wondering though, if it is possible to achieve the same without actually adding additional properties like IsChecked to the business objects. I was doing some thing like this with the wpf treeview, where I created a intermediate business object which has the IsChecked property and then based on the IsChecked value, create my actual business object.But I wanted to avoid it, because I'm not sure if it is the best practice.  Forgive for my ignorance, but could we use the CheckedItems, SelectedItems properties of the RadTreeView to bind to an observable collection and thus get the checked items? I know it is not possible directly because I tried, but is there a workaround?
Thanks again

Regards
Binu
0
Kiril Stanoev
Telerik team
answered on 27 Nov 2009, 09:32 AM
Hi Binu,

Actually the container bindings approach is very good since it is really close to the ViewModel concept from MVVM. The CheckedItems/SelectedItems collections will not work fully when the treeview is bound and an item is checked and it has not been visible yet. Then this item will not be in the Checked/SelectedItems collections.
Another way to retrieve the checked items in your business model is to use GetItemByPath() method of the treeview. This, however, requires additional functionality i.e. every item in the treeview must keep its path to the root level. I have attached a sample project that demonstrates the usage of GetItemByPath().
Finally, I'd suggest you use container bindigs for this particular scenario. Evaluate both approaches and I'd be glad if you share which path you have chosen.

Regards,
Kiril Stanoev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Binu Abraham
Top achievements
Rank 1
answered on 30 Nov 2009, 09:57 PM
Hi Kiril
Thanks for the alternative solution. I think we will go with the container bindings approach.
Thanks very much.

Regards
Binu
Tags
TreeView
Asked by
Binu Abraham
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Binu Abraham
Top achievements
Rank 1
Share this question
or