Getting checked items using MVVM

5 posts, 0 answers
  1. Binu Abraham
    Binu Abraham avatar
    6 posts
    Member since:
    Jul 2009

    Posted 25 Nov 2009 Link to this post

    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?
  2. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 25 Nov 2009 Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Binu Abraham
    Binu Abraham avatar
    6 posts
    Member since:
    Jul 2009

    Posted 25 Nov 2009 Link to this post

    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
  5. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 27 Nov 2009 Link to this post

    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.
  6. Binu Abraham
    Binu Abraham avatar
    6 posts
    Member since:
    Jul 2009

    Posted 30 Nov 2009 Link to this post

    Hi Kiril
    Thanks for the alternative solution. I think we will go with the container bindings approach.
    Thanks very much.

    Regards
    Binu
Back to Top
UI for WPF is Visual Studio 2017 Ready