2 Replies - 502 Views - Last Post: 26 September 2017 - 01:08 PM Rate Topic: -----

#1 ScottinTexas  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 261
  • Joined: 13-March 12

Subscribe to an event in a child view model

Posted 21 September 2017 - 06:25 AM

I have a MainWindowViewModel which derives from BaseWieModel. In htis view model I have a collection of InstrumentViewModels which derive from BaseViewModel (BaseViewModel has the INotifyPropertyChanged). The instrument view model has a collection of operator objects. I want the MainWindowViewModel to react to the selection changed event on the instrument view model operators. As I understand it, I have to subscribe to the PropertyChanged event of the InstrumentViewModel. So I did this just to generate squiggly red lines to help me get it right.
        InstrumentViewModel.PropertyChanged += OnOperatorChanged;

        public void OnOperatorChanged(object sender, PropertyChangedEventArgs e)
        {
            if(e.PropertyName=="OperatorSelected"){
                var op=sender as InstrumentViewModel;
                ReleaseHostages(op.InstrumentID);
            }
        }



In the ViewModelBase
        public event PropertyChangedEventHandler PropertyChanged;

        public virtual void RaisePropertyChanged([CallerMemberName]string propertyName = "")
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }



And in the InstrumentViewModel
        public Operator OperatorSelected
        {
            get { return _operator; }
            set
            {
                if (_operator != value)
                {
                    _operator = value;
                    RaisePropertyChanged("OperatorSelected");
                }
            }
        }



Would someone please tell me what is wrong?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Subscribe to an event in a child view model

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5889
  • View blog
  • Posts: 20,096
  • Joined: 05-May 12

Re: Subscribe to an event in a child view model

Posted 21 September 2017 - 06:22 PM

It would help if you told us what error you are getting.
Was This Post Helpful? 0
  • +
  • -

#3 ScottinTexas  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 261
  • Joined: 13-March 12

Re: Subscribe to an event in a child view model

Posted 26 September 2017 - 01:08 PM

I apologize for the long delay in replying. Thank you Skydiver for your reply. I wasn't getting an error. It just wasn't working. The InstrumentViewModel was working with an Instrument object. But the way I was using it I was pointing to an operator object in the View Model, not the operator object in the instrument object in the view model. I hope that is not too confusing.

It works now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1