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

Synchronizing SelectedItems between a RadDataGrid and a ViewModel

1 Answer 148 Views
Grid for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
jasonxz
Top achievements
Rank 1
jasonxz asked on 19 Jul 2013, 12:19 AM
What's the proper way to do this?  I see two MVVM-friendly ways of doing this in other XAML platforms (i.e. Silverlight & WPF) on the internet:
1.  Change the ItemContainerStyle:
<GridView.ItemContainerStyle>
    <Style TargetType="GridViewItem">
        <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}" />
    </Style>
</GridView.ItemContainerStyle>

2.  Create a custom Behavior.

Of course, I can't get the 1st option to work in RT.  I don't know why.
And RT doesn't support Behaviors.
Has anyone else done this or tried to?  I'd really not like to have to build some two-way synchronization code in the code-behind.

I understand that it's the established standard to make the SelectedItems property in a List control without a Setter, but, at this time I'd like to slap the person who decided that was a good idea (yes, I know it's an ObservableCollection & that would mess up the CollectionChanged event wirings -- but without being able to bind to the property, what's the point?).

1 Answer, 1 is accepted

Sort by
0
Ivaylo Gergov
Telerik team
answered on 22 Jul 2013, 12:27 PM
Hi Jason,

Thank you for using our RadControls for Windows 8 XAML.

Let me elaborate more on this scenario and specifically on the two approaches that you have suggested.
  1. The first approach does not work for you, because WinRT does not support read-only dependency properties, therefore the RadDataGrid.SelectedItems property is not a dependency property.

  2. About the second scenario, as you pointed out WinRT does not support behaviors out of the box. Nevertheless, there are open source solutions that mimics interactivity library functionality.

In this case,I can suggest implementing an attached behavior to the RadDataGrid that is TwoWay bound to a property from your ViewModel. Thus, you can get the RadDataGrid.SelectedItems collection when the behavior gets attached and then the VIewModel will handle the collection changed event.

For your convenience, I have also attached a project that shows how to achieve this.

Let me know if this helps.

Regards,
Ivaylo Gergov
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
Grid for XAML
Asked by
jasonxz
Top achievements
Rank 1
Answers by
Ivaylo Gergov
Telerik team
Share this question
or