简体   繁体   中英

How to raise a changed event in WPF on property of data item

I have an MVVM ViewModel which loads an EF object as part of the constructor, and then exposes that object as a property. The UI can then update depending on the properties of that object. So far so easy.

The UI has two date elements, handled with datepickers. When one changes, I want to examine the status of the other and possibly alert the user with a popup. So I created a "DateCheck" property on my ViewModel and bound the IsOpen property of the PopUp to that:

public bool Check
{
    get 
    {
        if (Job.ExtractDate != null && Job.DeliveryDate == null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

This should work in theory, but I can't figure out how to get it to trigger.

If I set the UpdateSourceTrigger of the binding to LostFocus, it never triggers because the binding is on the popup itself - it's not visible, so of course it can't lose focus.

If I set the UpdateSource trigger to PropertyChanged then I need to raise the change event somehow. But the property that's being changed isn't on the ViewModel itself - it's on the source object that the ViewModel is exposing as a property.

Certainly I'm missing something fundamental here and am being very stupid. How can I trigger my property change?

If you don't want to implement INotifyPropertyChanged in your model classes, then you could create two Date properties on your ViewModel that DO raise the PropertyChanged event.

You could simply map these two properties from your model class in your constructor.

Obviously, you'd have to copy the values back to your model object when you're done. Typically in a 'Save Changes' style of ICommand ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM