2

Parent collection is binded to a radGridView with hierarchical gridview structure.

Is there any way to trigger parent property when there is a change in inner collection

Parent Object:

    public class CoverHierarchical : EditableModelBase<CoverHierarchical>
    {
        #region Attribute Declarations

        private Int32 m_iId;
        private string m_sName = "";
        private bool m_bIsChanged;
        private ObservableCollection<CoverContentBO> m_oCoverContent;

        #endregion

        /// <summary>
        /// Get or set the id.
        /// </summary> 
        public Int32 Id
        {
            get { return this.m_iId; }

            set
            {
                if (this.m_iId != value)
                {
                    this.IsChanged = true;
                    this.m_iId = value;
                    RaisePropertyChanged("Id");
                }
            }
        }

        /// <summary>
        /// Get or set the name.
        /// </summary> 
        public string Name
        {
            get { return this.m_sName; }

            set
            {
                if (this.m_sName != value)
                {
                    this.IsChanged = true;
                    this.m_sName = value;
                    RaisePropertyChanged("Name");
                }
            }
        }


        /// <summary>
        /// Get or set the CoverContent Collection.
        /// </summary> 
        public ObservableCollection<CoverContentBO> CoverContentCollection
        {
            get { return this.m_oCoverContent; }

            set
            {
                if (this.m_oCoverContent != value)
                {
                    this.IsChanged = true;
                    this.m_oCoverContent = value;
                    RaisePropertyChanged("CoverContentCollection");
                }
            }
        }


        /// <summary>
        /// Get or set the changed flag.
        /// </summary>
        public bool IsChanged
        {
            get { return this.m_bIsChanged || this.CoverContentCollection.Any(i=>i.IsChanged); }

            set
            {
                if (this.m_bIsChanged != value)
                {
                    this.m_bIsChanged = value;
                    RaisePropertyChanged("IsChanged");
                }
            }
        }

        #endregion
  }

and the Child Object

        [Serializable]
    public class CoverContentBO : EditableModelBase<CoverContentBO>
    {
        #region Attribute Declarations

        private Int32 m_iCoverId;
        private Int32 m_iId;
        private bool m_bIsChanged;


        #endregion

        #region Public Properties

        /// <summary>
        /// Get or set the cover id.
        /// </summary> 
        public Int32 CoverId
        {
            get { return this.m_iCoverId; }

            set
            {
                if (this.m_iCoverId != value)
                {
                    this.IsChanged = true;
                    this.m_iCoverId = value;
                    RaisePropertyChanged("CoverId");
                }
            }
        }

        /// <summary>
        /// Get or set the id.
        /// </summary> 
        public Int32 Id
        {
            get { return this.m_iId; }

            set
            {
                if (this.m_iId != value)
                {
                    this.IsChanged = true;
                    this.m_iId = value;
                    RaisePropertyChanged("Id");
                }
            }
        }



        /// <summary>
        /// Get or set the changed flag.
        /// </summary>
        public bool IsChanged
        {
            get { return this.m_bIsChanged; }

            set
            {
                if (this.m_bIsChanged != value)
                {
                    this.m_bIsChanged = value;
                    RaisePropertyChanged("IsChanged");
                }
            }
        }

     #endregion

   }

In ViewModel

public ObservableCollection<CoverHierarchical> CoverHierarchicalCollection
    {
        get { return m_CoverHierarchicalCollection; }
        set
        {
            if (m_CoverHierarchicalCollection != value)
            {
                m_CoverHierarchicalCollection = value;
                OnPropertyChanged();
            }
        }
    }

CoverHierarchicalCollection is Bound to the view. If user edits the child grid, changes happen in child. Is there any way to trigger IsChanged property of parent, on changing the Child inner collection (CoverContentCollection) field.

Habeeb
  • 1,020
  • 16
  • 35
  • The question that I have marked your question as a duplicate of is somewhat more general than your question. However, you can still use its solution to inform your parent view model when your child view model changes. – Sheridan Nov 10 '14 at 09:41

1 Answers1

2

You can listen to PropertyChanged and CollectionChanged of child like below and update IsChanged of Parent

    /// <summary>
    /// Get or set the CoverContent Collection.
    /// </summary> 
    public ObservableCollection<CoverContentBO> CoverContentCollection
    {
        get { return this.m_oCoverContent; }

        set
        {
            if (this.m_oCoverContent != value)
            {
                this.IsChanged = true;

                RemoveEventhandlers(m_oCoverContent);

                this.m_oCoverContent = value;

                AddEventhandlers(m_oCoverContent);
                RaisePropertyChanged("CoverContentCollection");
            }
        }
    }

    private void AddEventhandlers(ObservableCollection<CoverContentBO> oCoverContent)
    {
        foreach (var CoverContentBO in oCoverContent)
        {
            CoverContentBO.PropertyChanged +=CoverContentBO_PropertyChanged;
        }

        oCoverContent.CollectionChanged +=oCoverContent_CollectionChanged;
    }

    void oCoverContent_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        foreach (CoverContentBO CoverContentBO in e.OldItems)
        {
            CoverContentBO.PropertyChanged -=CoverContentBO_PropertyChanged;
        }

        foreach (CoverContentBO CoverContentBO in e.NewItems)
        {
            CoverContentBO.PropertyChanged +=CoverContentBO_PropertyChanged;
        }
    }

    void CoverContentBO_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "IsChanged")
        {
            IsChanged = true;
        }
    }

    private void RemoveEventhandlers(ObservableCollection<CoverContentBO> oCoverContent)
    {
        foreach (var CoverContentBO in oCoverContent)
        {
            CoverContentBO.PropertyChanged -=CoverContentBO_PropertyChanged;
        }

        oCoverContent.CollectionChanged -=oCoverContent_CollectionChanged;
    }
Nitin
  • 18,344
  • 2
  • 36
  • 53