tags:

views:

35

answers:

2

I have an entity which inherits from INOTIFYPROPERTYCHANGED, this entity is bound to my UI elements like TextBOX. Now when any property is changed, my bindings are updated and everything works fine. What I want is to trap this propertychanged event in my viewmodal. my viewmodal also inherits from INOTIFYPROPERTYCHANGED.


Entity

public class DIPREDIPForView:INotifyPropertyChanged
{
    #region PrivateFields
    private string fieldDelimiter, textQualifier;

    #endregion
    public DIPREDIPForView()
    {

    }
    public string FieldDelimiter
    {
        get
        {
            return fieldDelimiter;
        }
        set
        {
            fieldDelimiter = value;
            OnPropertyChanged("FieldDelimiter");
            //NotifyViewModalEvent();
        }
    }
    public string TextQualifier {
        get
        {
            return textQualifier;
        }
        set
        {
            textQualifier = value;
            OnPropertyChanged("TextQualifier");
            //NotifyViewModalEvent();
        }
    }
    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;

    public virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
    #endregion

VIEWMODAL

public class DIPREDIPViewModel : BaseViewModel
{

    public DIPREDIPForView Configuration
    {
        get
        {
            return configuration;
        }
        set
        {
            configuration = value;               
        }
    }

}

BaseViewModal

public class BaseViewModel : INotifyPropertyChanged, IDisposable
{
    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;


    public virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
    #endregion


}

What i want is whenever "Configuration" changes,ViewModal should be notified as i have to call someother logic based on this.

I have tried calling OnPropertyChanged() in the setter of Configuration,but it doesnot fire. whenever i update a value on UI,This configuration is updated but breakpoint in the setter is not hit. How should i achieve this.

A: 

neither view model or view model base are subscribing to the PropertyChanged event. So therefore, neither of these objects will receive the event notification when its raised by DIPREDIPForView.

presumably DIPREDIPViewModel needs to subscribe to PropertyChanged on DIPREDIPForView, and the event subscription needs to point to the OnPropertyChanged event handler on BaseViewModel .

Not sure if this is what you are looking for.

Here is a sample...

[Test]
public void TestEventIsRaised()
{
    TestEventSubscriber subscriber = new TestEventSubscriber();
    subscriber.SetFirstName("joe");
    Assert.IsTrue(subscriber.EventWasHandledInTestDouble);
}


public class TestEventSubscriber : EventSubscriber
{
    public bool EventWasHandledInTestDouble;
    public TestEventSubscriber()
    {

    }

    protected override void OnMyEvent(object sender, EventArgs args)
    {
        EventWasHandledInTestDouble = true;
    }
}

public class EventSubscriber
{
    private EventRaiser eventRaiser = new EventRaiser();

    public EventSubscriber()
    {
        eventRaiser.MyEvent += (sender, args) => {
            Console.Write("Event was handled via anonymous delegate");
        };

        eventRaiser.MyEvent = OnMyEvent;
    }

    public void SetFirstName(string firstName)
    {
        eventRaiser.FirstName = "bob";
    }

    protected virtual void OnMyEvent(object sender, EventArgs args)
    {
        Console.WriteLine("Event was handled via virtual method...");
    }
}


public class EventRaiser
{
    public EventHandler MyEvent;

    public EventRaiser()
    {

    }

    private string firstName;
    public string FirstName
    {
        get
        {
            return firstName;
        }
        set
        {
            firstName = value;
            EventHandler handler = this.MyEvent;
            if (handler != null)
                MyEvent(this, EventArgs.Empty);
        }
    }

}
DevilDog74
just added some sample code...
DevilDog74
A: 

The ViewModel is a View of your Model.
I'd consider the following:

  1. Add a memeber of type DIPREDIPForView to class DIPREDIPViewModel
  2. Make DIPREDIPForView a model that does not implement INotifyPropertyChanged
  3. Expose properties: FieldDelimiter, TextQualifier, and Configuration all from DIPREDIPViewModel
  4. Set the DataContext of the View (XAML) to the ViewModel DIPREDIPViewModel for binding.

This keeps everything you need in one class, and makes updating multiple properties simpler.

Zamboni