| MAUI-CommunityToolkit-1 :👈 | 👉:MAUI-CommunityToolkit-3 |
π Hour 2 β ObservableObject, ObservableProperty & Property Change Notifications |
π Hour 2 β ObservableObject, ObservableProperty & Property Change Notifications
π 1. ObservableObject β The Core of MVVM Toolkit
ObservableObject is the base class that implements INotifyPropertyChanged for you.
Key Features:
- π’ Automatically raises PropertyChanged
- π§± Provides SetProperty() helper method
- β¨ Works with source generators for reduced boilerplate
Basic Example:
public class CounterViewModel : ObservableObject
{
private int count;
public int Count
{
get => count;
set => SetProperty(ref count, value);
}
}
β¨ 2. Using [ObservableProperty] Attribute
The [ObservableProperty] attribute auto-generates:
- βΎ A public property
- βΎ Backing field
- βΎ PropertyChanged notifications
Example:
public partial class UserViewModel : ObservableObject
{
[ObservableProperty]
private string username;
[ObservableProperty]
private int age;
}
This generates (automatically):
public string Username
{
get => username;
set => SetProperty(ref username, value);
}
π§² 3. Partial Methods β OnPropertyChanged Hooks
When you use [ObservableProperty], the toolkit generates methods for you:
- πΉ OnPropertyChanging
- πΉ OnPropertyChanged
You can write your own logic by adding partial methods.
Example:
public partial class ProfileViewModel
{
[ObservableProperty]
private string email;
partial void OnEmailChanged(string value)
{
Console.WriteLine("Email changed to: " + value);
}
}
π‘ Useful for validation, logging, event dispatching.
π 4. [NotifyPropertyChangedFor] β Dependent Properties
Sometimes one property affects another. For example:
- FullName depends on FirstName + LastName
Example:
public partial class PersonViewModel : ObservableObject
{
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(FullName))]
private string firstName;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(FullName))]
private string lastName;
public string FullName => $"{FirstName} {LastName}";
}
π Now whenever FirstName or LastName changes β FullName updates automatically!
π₯ 5. Using ObservableProperty with Validation
Example with simple validation:
public partial class LoginViewModel : ObservableObject
{
[ObservableProperty]
private string username;
partial void OnUsernameChanging(string value)
{
if (string.IsNullOrWhiteSpace(value))
Console.WriteLine("Username cannot be empty");
}
}
π‘ You can build real-time validations easily here.
π οΈ 6. Hands-On Practice for Hour 2 (Important)
- π Create a ViewModel using [ObservableProperty]
- π Add properties that depend on each other using [NotifyPropertyChangedFor]
- π§ͺ Add validation logic using OnXChanged or OnXChanging
- π Bind everything to your MAUI/WPF UI and test live updates
- π Use breakpoints to observe auto-generated property change calls
- π€ Display updated property values on UI when typing
π 7. Example: Hour 2 Practice ViewModel
This covers auto-properties, dependent properties, partial methods.
public partial class StudentViewModel : ObservableObject
{
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Details))]
private string name;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Details))]
private int marks;
public string Details => $"{Name} scored {Marks} marks";
partial void OnNameChanged(string value)
{
Console.WriteLine("Name updated to: " + value);
}
}
When Name or Marks changes:
- Updated value prints in console
- UI auto-updates
- Details property refreshes automatically
π Hour 2 Goals Achieved
- π― You understand ObservableObject deeply
- β‘ You can use [ObservableProperty] like a pro
- π You can create dependent UI properties
- π§ͺ You can add validation via partial methods
π Hour 2 β MVVM Toolkit Quiz
π Section A β Multiple Choice Questions
-
Which attribute automatically generates a property with PropertyChanged support?
a) [GeneratedProperty]
b) [ObservableProperty]
c) [AutoProperty]
d) [Bindable] -
What method does ObservableObject use internally to update a field?
a) UpdateValue()
b) RaiseChanged()
c) SetProperty()
d) Notify() -
Which feature allows you to run custom logic before or after a property changes?
a) PropertyChangedDelegate
b) OnInitialize()
c) Partial methods (OnXChanging / OnXChanged)
d) PropertyInterceptor -
Which attribute updates a dependent property automatically?
a) [DependsOn]
b) [NotifyPropertyChangedFor]
c) [LinkedProperty]
d) [CascadeProperty] -
When using [ObservableProperty], what is generated by the toolkit?
a) Only getters
b) Only setters
c) Full property + change notifications
d) Nothing (you must write manually)
π§ Section B β True or False
- True / False: ObservableObject already implements INotifyPropertyChanged.
- True / False: Partial methods like OnNameChanged are optional.
- True / False: [NotifyPropertyChangedFor] can only be used on methods, not properties.
- True / False: [ObservableProperty] generates a private field and a public property.
- True / False: If FirstName changes, dependent properties must be manually updated.
βοΈ Section C β Short Answer Questions
- Explain what [ObservableProperty] does and give an example field using it.
- What problem does [NotifyPropertyChangedFor] solve? Give an example.
- Write a partial method that logs when a property called βScoreβ changes.
- Describe the purpose of SetProperty() inside ObservableObject.
- What are OnXChanging and OnXChanged methods? When would you use them?
π» Section D β Write the Output
Given this code:
public partial class PlayerViewModel : ObservableObject
{
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Status))]
private int level;
public string Status => $"Level: {Level}";
}
- What happens in the UI when Level changes?
- Does Status update automatically?
- Which method is called behind the scenes?
| MAUI-CommunityToolkit-1 :👈 | 👉:MAUI-CommunityToolkit-3 |