How to modify SelectedItems in the DataGrid in the ViewModel

1 Answer 298 Views
Buttons DataGrid
Boris
Top achievements
Rank 1
Boris asked on 29 Sep 2022, 08:59 PM

The SelectedItems in the Documentation is an ObservableCollection but it only has a getter and no setter.

I am able to get SelectedItems like this using interactivity behavior by passing SelectedItems as a command parameter to the ViewModel

But I would like also be able to modify SelectedItems in the ViewModel as well
<telerikGrid:RadDataGrid x:Name="UserGrid"
                                     ItemsSource="{x:Bind ViewModel.Source}"
                                     AutoGenerateColumns="False"
                                     RowHeight="25"
                                     SelectionMode="Multiple"
                                     SelectionUnit="Row"
                                     UserGroupMode="Disabled">
                <i:Interaction.Behaviors>
                    <ic:EventTriggerBehavior EventName="SelectionChanged">
                        <ic:InvokeCommandAction
                            Command="{x:Bind ViewModel.SelectedUsersCommand}"
                            CommandParameter="{x:Bind UserGrid.SelectedItems,Mode=OneWay}"/>
                    </ic:EventTriggerBehavior>
                </i:Interaction.Behaviors>
                <telerikGrid:RadDataGrid.Columns>
                    <telerikGrid:DataGridTemplateColumn SizeMode="Fixed" Width="10">
                        <telerikGrid:DataGridTemplateColumn.CellContentTemplate>
                            <DataTemplate>
                                <StackPanel VerticalAlignment="Center" Margin="5 0 0 0">
                                    <CheckBox/>
                                </StackPanel>
                            </DataTemplate>
                        </telerikGrid:DataGridTemplateColumn.CellContentTemplate>
                    </telerikGrid:DataGridTemplateColumn>
                    <telerikGrid:DataGridTextColumn x:Uid="NameColumn" SizeMode="Auto"  PropertyName="UserName"/>
                    <telerikGrid:DataGridTextColumn x:Uid="EmailColumn" SizeMode="Auto"  PropertyName="UserEmail"/>
                    <telerikGrid:DataGridTextColumn x:Uid="DepartmentColumn" SizeMode="Auto"  PropertyName="DepartmentName"/>
                    <telerikGrid:DataGridBooleanColumn x:Uid="UserStatusColumn" SizeMode="Auto" PropertyName="UserActiveIndicator"/>
                    <telerikGrid:DataGridDateColumn x:Uid="DateCreatedColumn" SizeMode="Auto" PropertyName="RecordCreated" CellContentFormat="{}{0:d}"/>
                    <telerikGrid:DataGridDateColumn x:Uid="DateModifiedColumn" SizeMode="Auto" PropertyName="RecordModified" CellContentFormat="{}{0:d}"/>
                </telerikGrid:RadDataGrid.Columns>
            </telerikGrid:RadDataGrid>

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 30 Sep 2022, 09:27 AM

Hello Boris,

The SelectedItems collection from the selector pattern used in WinUI are always read-only. There isn't a way to directly data bind the property to a collection, but there is a behavior that can be created to sync the SelectedItems of the DataGrid with the collection in the view model. You can find this type of implementation in the following example: https://github.com/telerik/xaml-sdk/tree/master/GridView/BindingSelectedItemsFromViewModel

Note that the example is for WPF, but the API is the same, so it can be used almost without changes in WinUI too. For your convenience, I've attached a small WinUI example showing this approach. I hope that helps.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Buttons DataGrid
Asked by
Boris
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or