Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
920
XamDataGrid - Clearing the selection
posted

Hi,

   My page has a xamdatagrid control and some radio buttons (not on the xamdatagrid). After I select a record in xamdatagrid and switch to a certain radio button, the selected record in xamdatagrid should get unselected. What should I do to unselect the selected row ( the highlight on the selected row should go away). I am using MVVM and I am setting the SelectedItem to null when I switch to the certain radio button. I have an event handler in code behind OnSelectedItemsChanged(object sender, SelectedItemsChangedEventArgs e) to do some processing

x:Name="ResultsGrid"
DataSource = "{Binding Path=..., Mode=OneWay}"
SelectedDataItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsNestedDataDisplayEnabled="true"
IsGroupByAreaExpanded="false"
IsSynchronizedWithCurrentItem ="True"
GroupByAreaLocation="None" SelectedItemsChanged="OnSelectedItemsChanged"
DataContextChanged="OnDataContextChanged"
>

private void OnSelectedItemsChanged(object sender, SelectedItemsChangedEventArgs e)
{
var viewModel = DataContext as ...;

var grid = sender as XamDataGrid;
if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Records != null)
{
if (grid.SelectedItems.Records.Count == 1 && grid.SelectedItems.Records[0].IsDataRecord)
{
if (((DataRecord)grid.SelectedItems.Records[0]).DataItem is ...)
{
...
}
}

}

Thanks,

Lalasa

  • 2490
    Verified Answer
    Offline posted


    Hello Lalasa,

    Thank you for the code snippet you have provided.

    If you want to clear all selected records from the grid you can clear the Selected Items Record collection:

    grid.SelectedItems.Records.Clear();

    And if you want to unselect a specific record you can set its IsSelected property to false:

    grid.Records[1].IsSelected = false;

    Additionally if you want the highlight of the selected record to go away you will also have to clear the ActiveRecord:

    grid.ActiveRecord = null;

    If you have further questions concerning this matter, please let me know.