wpfbindingobservablecollectioninotifypropertychangedxamdatagrid

add functionality to WPF observable collections item changed event without overriding it


I am new to WPF, and I am struggling on adding functionality on observable collections.

I have dropbox, datagrid and a list box.

Data grid and list boxes are binded to corresponding observable collections.

I want to add a listener to dropbox and the items in the list views.

Here is my sample code

XAML

     <controls:SearchableDropBox DomainObject="{Binding ClassA}"                            
InputValidationManifest="{Binding ClassValidation, Mode=OneWay}"/>


         <Custom:XamDataGrid DataSource="{Binding Path=ClassB.ClassList}"                        
GroupByAreaLocation="None" />

     <controls:ListBox x:Name="listBoxInputs" ItemsSource="{Binding ClassC, Mode=oneWay}">

VIEMMODEL

 class MainViewModel : ViewModel
    {
        #region Attributes

        private classA _classa;
        private ObservableCollection<classC> _classC;
        private classB;

        #endregion

        public MainViewModel()
        {

            this._classc= new ObservableCollection<classC>();
           this._classB = new classB()


        }

public classA ClassA
        {
            get { return _classA; }
            set
            {
                if (!Equals(value, _classA))
                {

                    _classA= value;
                    SelectionChanged(); //This is the function I want add on //propertychanged event
                    OnPropertyChanged(nameof(classA));
                }

            }
        }

        public ObservableCollection<classC> ClassC
    {
        get { return classC; }
        set
        {
            this.classC= value;
            OnPropertyChanged(nameof(ClassC));
        }
    }

ClassB

 public class classB
    {
        private ObservableCollection<String> _classList;

    }

I want to call SelectionChanged() function when the selection of the dropbox, and when item added/deleted in list box without overriding their current notify property changed events.


Solution

  • For add/Remove in _classC collection, you subscribe to CollectionChanged event.

         public MainViewModel()
        {
            this._classc= new ObservableCollection<classC>();
            this._classB = new classB()
           _classC.CollectionChanged += _classC_CollectionChanged;
        }
    
         private void _classC_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            //Populate ClassB.ClassList / Datagrid Function Call
        }