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