0

I have the following codes:

One of my viewmodel's properties:

    private ObservableCollection<ArukeresoShopMediator<T>> _shops;
    public ObservableCollection<ArukeresoShopMediator<T>> Shops { ... }

ArukeresoShopMediator class:

    class ArukeresoShopMediator<T>
        where T : CheckArukeresoArCriteriaType, INotifyPropertyChanged
    {
        public Shop Shop { ... }

        private T _data;
        public T Data { ... }

        public string Key { ... }
        public string Name { ... }

        private bool _checked;
        public bool Checked { ... }

        public ArukeresoShopMediator(T data, Shop shop) { ... }
    }

UI part of the code:

        <ListBox Grid.Row="2" 
                 Grid.ColumnSpan="2"
                 Visibility="{Binding CompaniesVisibility}"
                 ItemsSource="{Binding Shops, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Checked}" Content="{Binding}"></CheckBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

My problem is that there's a function that i'd like to execute within my viewmodel's Shops property's setter when the Checkbox property is changed but currently i have no idea how to do that.

As you can see Shops property is an ObservableCollection of type ArukeresoShopMediator which has a boolean Checked property. How can i make my Shops property respond to the Checked property's changes?

  • As a note, setting `Mode=TwoWay` and `UpdateSourceTrigger=PropertyChanged` on the ItemsSource Binding is pointless. It has no effect and should be removed. Besides that, do you mean the setter of the `Checked` property in your view model isn't called when you toggle the CheckBox? – Clemens Sep 27 '19 at 07:35
  • Do you want to monitor for property changes of items inside observable collection? Then you have to subscribe to every item event, see [this](https://stackoverflow.com/q/901921/1997232). – Sinatr Sep 27 '19 at 07:35
  • @Clemens Setter of the Checked property is called when i toggle the CheckBox. I mean i'd like to call the setter of the Shops when a CheckBox is toggled. – Foltán Dániel Sep 27 '19 at 07:37
  • That makes no sense. Why should that even happen? The Shops property doesn't change when a property of one of its items changes. It's still the same collection. – Clemens Sep 27 '19 at 07:38
  • I'd like to make something that @Sinatr mentioned above. Sorry for my poor explanation. – Foltán Dániel Sep 27 '19 at 07:41
  • Sounds like you should move the method (or its invocation) from the `Shops` setter to the `Checked` property. A change of `Checked` is actually trigger and not `Shops`... – BionicCode Sep 27 '19 at 08:26

1 Answers1

0

As i mentioned in the comments above i needed something like the post mentioned by Sinatr, so i found a working solution and it looks like this:

        void items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.OldItems != null)
            {
                foreach (ArukeresoShopMediator<T> item in e.OldItems)
                {
                    item.PropertyChanged -= item_PropertyChanged;
                }
            }
            if (e.NewItems != null)
            {
                foreach (ArukeresoShopMediator<T> item in e.NewItems)
                {
                    item.PropertyChanged += item_PropertyChanged;
                }
            }
        }

        void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            ...
        }

        public ArukeresoCriteriaTypeInspectorViewModelBase(T data)
            : base(data)
        {
            ...
            Shops = new ObservableCollection<ArukeresoShopMediator<T>>();
            Shops.CollectionChanged += items_CollectionChanged;
            ...