Search code examples
c#wpfdatagridselectionwpfdatagrid

Programmatically deselect a row in a DataGrid


I'm struggling to find a solution to this.

What I am trying to do is to only make certain rows selectable in a DataGrid. The SelectionMode is FullRow. An example would be if a user tried to drag-select a few rows, and one of them I wouldn't want to be selectable. In this instance I would want the valid rows to be selected still, but not the invalid one.

Any ideas?


Solution

  • This guy wanted to do something alike with a ListBox. I believe the solution can be adapted to work with the DataGrid as well.

    EDIT

    public static class DataGridRowEx
    {
        public static bool GetCanSelect(DependencyObject obj)
        {
            return (bool)obj.GetValue(CanSelectProperty);
        }
        public static void SetCanSelect(DependencyObject obj, bool value)
        {
            obj.SetValue(CanSelectProperty, value);
        }
        public static readonly DependencyProperty CanSelectProperty =
            DependencyProperty.RegisterAttached("CanSelect", typeof(bool), typeof(DataGridRowEx), new UIPropertyMetadata(true, OnCanSelectChanged));
    
        private static void OnCanSelectChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var item = sender as DataGridRow;
            if (item == null)
                return;
    
            if ((bool)args.NewValue)
            {
                item.Selected -= RowSelected;
            }
            else
            {
                item.Selected += RowSelected;
                item.IsSelected = false;
            }
        }
    
        private static void RowSelected(object sender, RoutedEventArgs e)
        {
            var item = sender as DataGridRow;
            if (item == null)
                return;
    
            item.Dispatcher.BeginInvoke((Action)(()=>
            item.IsSelected = false));
        }
    }
    

    To test it:

    public class ViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged values
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    
    
        public List<Dummy> Elements { get; set; }
    
        public ViewModel()
        {
            this.Elements = new List<Dummy>(){
                new Dummy() { CanSelect =true, MyProperty = "Element1"},
                new Dummy() { CanSelect =false, MyProperty = "Element2"},
                new Dummy() { CanSelect =true, MyProperty = "Element3"},
                new Dummy() { CanSelect =false, MyProperty = "Element4"},
                new Dummy() { CanSelect =true, MyProperty = "Element5"},
                new Dummy() { CanSelect =true, MyProperty = "Element6"},
                new Dummy() { CanSelect =true, MyProperty = "Element7"},
                new Dummy() { CanSelect =true, MyProperty = "Element8"},
                new Dummy() { CanSelect =false, MyProperty = "Element9"},
            };
        }
    }
    
    public class Dummy
    {
        public bool CanSelect { get; set; }
    
        public string MyProperty { get; set; }
    
        public override string ToString()
        {
            return this.MyProperty;
        }
    }
    
    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Width="525"
        Height="350"
        mc:Ignorable="d">
    <Window.Resources>
        <Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}">
            <Setter Property="local:DataGridRowEx.CanSelect" Value="{Binding CanSelect}" />
        </Style>
    </Window.Resources>
    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>
    <Grid x:Name="LayoutRoot">
        <DataGrid ItemsSource="{Binding Elements}"
                  RowStyle="{DynamicResource DataGridRowStyle}"
                  SelectionUnit="FullRow" />
    </Grid>
    </Window>
    

    It works with multiselection, even when pressing shift. The only significant difference from the solution for the ListBoxItem was that the unselection had to be queued using Dispatcher.BeginInvoke, not sure why. The only caveat with this approach is that trying to select an unselectable item unselects the current selected one if the DataGrid has single selection, or unselects all, if the DataGrid has extended selection.