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
430
Bound Checkbox seems to lose data binding set functionality
posted

I am working on a page with two tables: one to show all the items, and one to show only the selected items. An item can added to/removed from the selected list by a checkbox in either table that is bound to a boolean in the view models. The view models notify other entities in the page that an item has been selected or deselected by way of a delegate that is passed in on construction of the view model:

public bool ItemViewModel.Selected
{
     get { return this.selected; }
     set
     {
          if (this.itemSelectedChangedHandler != null && value != this.selected)
          {
               this.selected = value;
               this.itemSelectedChangedHandler(this.item, this.selected);
          }
     }
}

When an item is selected/deselected, the selected list will update by hiding non-selected items and showing selected items. It will also call a method to set the selected value of the view model without invoking the itemSelectedChangedHandler:

private void PageViewModel.UpdateSelectedItemsTable()
{
     foreach (var row in this.selectedItemsTable.Rows)
     {
          var itemViewModel = row.ListObject as ItemViewModel;

          if (this.dataModule.SelectedItems.Contains(itemViewModel.Item))
          {
               itemViewModel.Select(true);
               itemViewModel.UpdateChildren();
               row.ExpandAll();
               row.Hidden = false;
          }
          else
          {
               itemViewModel.Select(false);
               row.CollapseAll();
               row.Hidden = true;
          }
     }

}

public void ItemViewModel.Select(bool selected)
{
     if (this.selected != selected)
     {
          this.selected = selected;
          this.OnPropertyChanged(nameof(this.Selected));
     }
}

I can successfully select and deselect items from the table of all items (which has a different view model but ends up calling the same UpdateSelectedItemsTable), however, when I try to deselect an item from the selected table, I can deselect the first item, but subsequent items will not enter into their set accessors when I click their checkbox. To make debugging even more confusing, if I have a breakpoint on the set accessor from the beginning, before I select or deselect any items, I can successfully deselect all items by clicking their checkbox in the selected items table. I have no idea why the same code would have different outcomes when I step through it and when I don't.

Any thoughts or suggestions would be appreciated at this point!

Thank you!

Parents Reply Children