By default, I get cell highlighting on MouseOver and cell selection on Click. I don't want either of those. When I click on any cell, I want the entire row selected and I don't want the RowSelector visible.
When the row is selected, I need to then call a command on the View's corresponding ViewModel using MVVM Light toolkit's EventToCommand.
In the grid, set the selection settings:
<igGrid:XamWebGrid.SelectionSettings> <igGrid:SelectionSettings CellSelection="None" CellClickAction="SelectRow" ColumnSelection="None" RowSelection="Single"/> </igGrid:XamWebGrid.SelectionSettings>
and handle the SelectedRowsCollectionChanged event:
SelectedRowsCollectionChanged="itemsSummaryGrid_SelectedRowsCollectionChanged"
private void itemsSummaryGrid_SelectedRowsCollectionChanged(object sender, SelectionCollectionChangedEventArgs<SelectedRowsCollection> e) { if (e.NewSelectedItems.Count == 1) { Messenger.Default.Send<ItemEntity>((ItemEntity)e.NewSelectedItems[0].Data); } }
Instead of RelayCommand in MVVM Light toolkit, I use Messenger to send the selected item.
Another option is to go with the more generic and pure delcarative approach which comes in handy in MVVM projects. Here's what I did... I really like it because it is very generic and I can reuse the same behavior on as many grids that use different collections.
Behavior:public class IgGridControlRowSelectedBehavior : Behavior<XamGrid> { protected override void OnAttached() { base.OnAttached(); if (AssociatedObject != null) AssociatedObject.SelectedRowsCollectionChanged += SelectedRowsCollectionChanged; }
protected override void OnDetaching() { if (AssociatedObject != null) AssociatedObject.SelectedRowsCollectionChanged -= SelectedRowsCollectionChanged; base.OnDetaching(); } void SelectedRowsCollectionChanged(object sender, SelectionCollectionChangedEventArgs<SelectedRowsCollection> e) { if (e.NewSelectedItems.Count == 1) {
// get reference to the selected row in the grid. var rowData = e.NewSelectedItems[0].Data;
// let anyone listening for a new selected item know it was selected if (rowData != null) Messenger.Default.Send(rowData, rowData.GetType()); } }}
Grid:<ig:XamGrid x:Name="CourseGrid" ItemsSource="{Binding Path=CourseCollectionViewSource.View}" AutoGenerateColumns="False"> <ig:XamGrid.SelectionSettings> <ig:SelectionSettings CellClickAction="SelectRow" /> </ig:XamGrid.SelectionSettings> <ig:XamGrid.Columns>[...]</ig:XamGrid.Columns> <i:Interaction.Behaviors> <myBehaviors:IgGridControlRowSelectedBehavior /> </i:Interaction.Behaviors></ig:XamGrid>
Where the namespace "i" = xmlns:i=http://schemas.microsoft.com/expression/2010/interactivity
View Model listening for selected items:Messenger.Default.Register<object>(this, typeof(Course), message => SelectedCourse = message as Course);