How to call a method after a property update
Asked Answered
D

1

8

I have a DatePicker control bound to viewmodel.SelectedDate. Rather than using propfull I am using the CTK [ObservableProperty]. When I select a new date I want to call another function that gets a fresh dataset based on that new date. Is there another annotation for that?

    /// Set by the Date Control on the form
    [ObservableProperty]
    //[AlsoCallThisFunction(DisplayBookings)]
    public DateTime bookingDate;

    ///I want to call this for a fresh dataset
    ///after the bookingDate is set
    void DisplayBookings()
    {
        GoToDatabaseAndGetNewRecordset(bookingDate);
    }

Old way of doing it:

    //private DateTime bookingDate;

    //public DateTime BookingDate
    //{
    //    get { return bookingDate; }
    //    set { 
    //        bookingDate = value;
    //        DisplayBookings();

    //    }
    //}
Detta answered 13/4, 2022 at 12:55 Comment(2)
ahh pity. I was hoping for an annotation.Detta
with Fody.PropertyChanged library you may use On<PropertyName>Changed methods like public DateTime BookingDate { get; set; } private void OnBookingDateChanged() { .. body or call of DisplayBookings ... } andWide
P
10

You can override OnPropertyChanging and OnPropertyChanged events, and call your method there.

Just keep in mind that if you set your binding as UpdateSourceTrigger=PropertyChanged maybe some changes are still going on.

protected override void OnPropertyChanged(PropertyChangedEventArgs e)
{
    base.OnPropertyChanged(e);

    if (e.PropertyName == nameof(BookingDate))
    {
        DisplayBookings();
    }
}

UPDATE

Starting at v8.0.0 Preview 3 you can use Partial property change methods.

Quoted from the blog:

When using [ObservableProperty] to generate observable properties, the MVVM Toolkit will now also generate two partial methods with no implementations: On<PROPERTY_NAME>Changing and On<PROPERTY_NAME>Changed. These methods can be used to inject additional logic when a property is changed, without the need to fallback to using a manual property. Note that because these two methods are partial, void-returning and with no definition, the C# compiler will completely remove them if they are not implemented, meaning that when not used they will simply vanish and add no overhead to the application

Then you could rewrite it as:

[ObservableProperty]
public DateTime bookingDate;

partial void OnBookingDateChanged(DateTime bookingDate)
{
    DisplayBookings();
}
Perutz answered 13/4, 2022 at 13:8 Comment(9)
Is that going in the form's code-behind or the view-model?Detta
In the ViewModel where you has your ObservablePropertyPerutz
Ok, so in CTK Preview 3 so far the annotation I was hoping for is not there. I've put in a feature request on the CTK GitHub. I'll use your workaround for the time being - Thanks!Detta
I'm waiting for [Required], [MinLength()] and [MaxLenght()] annotations too.Perutz
Did you post a request on the CTK GitHub? Of course you'll have to let them know. github.com/CommunityToolkit/WindowsCommunityToolkit/discussionsDetta
I think it's coming into the next releasePerutz
FYI: A man over on the CTK Github took it another layer cleaner: [ObservableProperty] private DateTime _bookingDate; partial void OnBookingDateChanged(DateTime bookingDate) { DisplayBookings(); }Detta
Yes, you can have a look here but you need v8.0.0 Preview 3Perutz
The revised approach mentioned is working for me with Community.Toolkit.Mvvm version 8.2.2Equivocate

© 2022 - 2024 — McMap. All rights reserved.