-1

I have a DataGrid and bind its item source to the below class list. How to bind the Data grid rows IsSelected property to the class IsChecked property ?

public class DeckInt : INotifyPropertyChanged
    {
        public int id { get; set; }
        public string name { get; set; }
        public bool isChecked { get; set; }


        public int Id { get { return id; } set { id = value; OnPropertyChanged(nameof(Id)); } }
        /// <summary>
        /// Group name of the weight item
        /// </summary>
        public string Name { get { return name; } set { name = value; OnPropertyChanged(nameof(Name)); } }
        public bool IsChecked { get { return isChecked; } set { isChecked = value; OnPropertyChanged(nameof(IsChecked)); } }

        public event PropertyChangedEventHandler PropertyChanged;

        //When propert changes, notify
        protected virtual void OnPropertyChanged(string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
nihasmata
  • 652
  • 1
  • 8
  • 28
  • 1
    https://stackoverflow.com/q/23441113/1136211 – Clemens Jul 07 '22 at 08:20
  • The above link provides seriously wrong information. Do not disable UI virtualization to fix an alleged bug! The DataGrid has no bug. If you feel you have a problem when selecting virtualized items (by selecting rows via their model, like in your case by setting IsChecked property), please refer to this answer: https://stackoverflow.com/a/72927271/3141792 – BionicCode Jul 10 '22 at 09:13

1 Answers1

0

You must configure the Binding by defining a Style that targets DataGridRow:

<DataGrid>
  <DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
      <Setter Property="IsSelected"
              Value="{Binding IsChecked}" />
  </DataGrid.RowStyle>
</DataGrid>
BionicCode
  • 1
  • 4
  • 28
  • 44