2

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();

    //    }
    //}
Ray Brennan
  • 373
  • 1
  • 6
  • 19
  • ahh pity. I was hoping for an annotation. – Ray Brennan Apr 13 '22 at 13:05
  • with [`Fody.PropertyChanged` library](https://github.com/Fody/PropertyChanged) you may use `OnChanged` methods like `public DateTime BookingDate { get; set; } private void OnBookingDateChanged() { .. body or call of DisplayBookings ... } ` and – Selvin Apr 13 '22 at 14:36

1 Answers1

4

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();
}
McNets
  • 10,352
  • 3
  • 32
  • 61
  • Is that going in the form's code-behind or the view-model? – Ray Brennan Apr 13 '22 at 13:34
  • 1
    In the ViewModel where you has your ObservableProperty – McNets Apr 13 '22 at 13:35
  • 1
    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! – Ray Brennan Apr 13 '22 at 13:53
  • I'm waiting for [Required], [MinLength()] and [MaxLenght()] annotations too. – McNets Apr 13 '22 at 13:56
  • Did you post a request on the CTK GitHub? Of course you'll have to let them know. https://github.com/CommunityToolkit/WindowsCommunityToolkit/discussions – Ray Brennan Apr 14 '22 at 14:14
  • I think it's coming into the next release – McNets Apr 14 '22 at 14:16
  • FYI: A man over on the CTK Github took it another layer cleaner: [ObservableProperty] private DateTime _bookingDate; partial void OnBookingDateChanged(DateTime bookingDate) { DisplayBookings(); } – Ray Brennan Apr 15 '22 at 12:57
  • Yes, you can have a look [here](https://devblogs.microsoft.com/ifdef-windows/announcing-net-community-toolkit-v8-0-0-preview-3/) but you need v8.0.0 Preview 3 – McNets Apr 16 '22 at 11:11