0

I have a table with checkboxes that is bound to the ObservableCollection > collection, I want to track changes to this collection when one of the checkboxes changes my view.

This is my code:

<UserControl.Resources>
<DataTemplate x:Key="DataTemplate_Level2">
  <CheckBox IsChecked="{Binding Path=. ,Mode=TwoWay}" Height="40" Width="50" Margin="4,4,4,4"/>
</DataTemplate>
<DataTemplate x:Key="DataTemplate_Level1">
  <ItemsControl x:Name="2st" Items="{Binding Path=. ,Mode=TwoWay}" ItemTemplate="{DynamicResource DataTemplate_Level2}">
    <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal"/>
      </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
  </ItemsControl>
</DataTemplate>
</UserControl.Resources>
<ItemsControl Grid.Column="1" Items="{Binding MyCollection, Mode=TwoWay}" x:Name="lst" ItemTemplate="{DynamicResource DataTemplate_Level1}" Background="Gold"/>

My viewModel property

public ObservableCollection<ObservableCollection<bool>> MyCollection
{
        get
        {
            return someCollection;
        }

        set
        {
            someCollection = value;
            RaisePropertyChanged(nameof(MyCollection));
        }
 }

view of table

How Can I pass collection data changes to view model?

  • Welcome to SO. So, exactly, which one is your question? Keep in mind that SO is not a free coding service. – David García Bodego Oct 17 '19 at 03:36
  • My question is how to pass collection change data to the view model – Даниил Мельников Oct 17 '19 at 03:42
  • _"I want to track changes to this collection when one of the checkboxes changes my view"_ -- that's a specification, not a question. Please provide a good [mcve] that shows clearly what you've tried already, explain precisely what that code does, what you want it to do instead, and what _specifically_ you can't figure out and need help with. – Peter Duniho Oct 17 '19 at 03:44
  • In the property you are not setting the value to your collection in the setter. I think you are missing this line `someCollection = value` in the setter – Aakanksha Oct 17 '19 at 04:43

1 Answers1

0

You need to declare a new class that will become viewmodel for checkbox with property of type book and proper RaisePropertyChanged invoking. And MyCollection must be collection of collections of instances of that class rather than bool

public class CheckboxViewModel
{
   private bool _checkboxValue;

   public bool CheckboxValue
   {
      get
      {
         return _checkboxValue;
      }
      set
      {
         _checkboxValue = value;
         RaisePropertyChanged(nameof(CheckboxValue));
      }
   }
}

Make sure you have two-way binding in checkbox view to that property

BTW - RaisePropertyChanged at setter of MyCollection raises event with wrong property name in you example.