Ren
Ren

Reputation: 437

WPF DataGrid SelectAll Checkbox with two-way binding

I'm binding a DataGrid to a List, which has a boolen property "IsSelected".

public partial class MainWindow : Window
{
  List<BaselineEntity> _blRecs;

  public MainWindow()
  {
    InitializeComponent();

    _blRecs = new List<BaselineEntity>();

    _blRecs.Add(new BaselineEntity{EntityId = "Yada_Yada_1", IsSelected = false});
    _blRecs.Add(new BaselineEntity{EntityId = "Yada_Yada_2", IsSelected = false});
    _blRecs.Add(new BaselineEntity{EntityId = "Yada_Yada_2", IsSelected = false});

    ChangedBlRecsGridView.ItemsSource = _blRecs;
  }
}

Where the class BaselineEntity is defined as:

internal class BaselineEntity
{
    public string EntityId {get; set;}
    public bool IsSelected { get; set; }
}

The XAML is this

<DataGrid Name="ChangedBlRecsGridView" 
          ItemsSource="{Binding _blRecs, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Entity Id" Binding="{Binding EntityId}"></DataGridTextColumn>

        <DataGridTemplateColumn Width="30">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.Header>
                <CheckBox Name="chkSelectAll" Checked="chkSelectAll_Checked" Unchecked="chkSelectAll_Unchecked"></CheckBox>
            </DataGridTemplateColumn.Header>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Where the event handlers for the checked and Unchecked events of the Select-All checkbox are like this:

private void chkSelectAll_Checked(object sender, RoutedEventArgs e)
{
    foreach (BaselineEntity enty in ChangedBlRecsGridView.ItemsSource)
    {
        enty.IsSelected = true;
    }
}

private void chkSelectAll_Unchecked(object sender, RoutedEventArgs e)
{
    foreach (BaselineEntity enty in ChangedBlRecsGridView.ItemsSource)
    {
            enty.IsSelected = false;
    }
}

I'm familiar with MVVM and I know I haven't stuck to any best practices (like implementing a viewModel), cuz I'm just getting started with WPF.

The Check and Uncheck event handlers set and reset the IsSelected Property, but the view is NOT getting updated about this change. It looks like the intended 2-way binding isn't happening somehow.

My guess is that I should have implemented the BaselineEntity class with some interface to push notifications to the view (This is what I'd expect from my somewhat working knowledge on knockout js, but couldn't find what it is after spending hours).

I don't know whatelse I'm missing here.. I just could NOT find what could be the solution.

Any help/guidance is appreciated..

Thank you!

Upvotes: 2

Views: 642

Answers (1)

Ayyappan Subramanian
Ayyappan Subramanian

Reputation: 5366

Refer the below model code with INotifyPropertyChanged

 internal class BaselineEntity:INotifyPropertyChanged
{
    public string EntityId { get; set; }

    private bool isSelected;

    public bool IsSelected
    {
        get { return isSelected; }
        set { isSelected = value; OnPropertyChanged("IsSelected"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

Upvotes: 2

Related Questions