This article is part of the MVVM – Made Easy series, with a focus on the initial preview release of v8.4 of the CommunityToolkit.Mvvm (aka Microsoft MVVM Toolkit) NuGet package, which includes support for Partial Properties.
Update as of Dec 2024: The v8.4 stable release is now available. For further details, please refer to this blog post.
Here are the links to other articles in this series.
Support for Partial Properties is one of most sought after features in the MVVM toolkit as it allows to define custom access modifier for property accessors and makes code more readable. The need for an analyzer to track the usage of a field instead of a (generated) property is no longer necessary.
Background:
This feature is basically a blend of partial and semi-auto properties. Similar to partial methods, the definition and implementation of the property are decoupled denoted by the partial keyword. The semi-auto properties allow to refer to the (compiler generated) backing field with a contextual keyword named field. On the whole, it’s an optimal candidate for source generation.
Package Install:
Run the below command from the CLI or search for the mvvm toolkit in the NuGet Package Manager within the IDE.
Install with the --prerelease option to get the latest preview release of the NuGet package.
dotnet add package CommunityToolkit.Mvvm
Note: For the sake of clarity, I have limited the generated code in the examples to the bare minimum. In reality, it’s much more.
Before:
It should be noted that both the getter and setter possess the same accessibility level as the property.
public partial class BaseViewModel : ObservableObject
{
[ObservableProperty]
private string? _title;
}
// Generated code
partial class BaseViewModel
{
public string? Title
{
get => _title;
set
{
// Verify if the value has indeed changed
_title = value;
// Raise the OnPropertyChanged notification
}
}
}
After:
Now, it is possible to specify custom accessibility levels for property accessors, both getters and setters. Field declarations are no longer necessary. Notice the partial keyword in the property definition. There’re limitations which is covered at the conclusion.
Technically, a getter can have an access specifier that is not public, within the context of MVVM, this does not make much sense.
public partial class BaseViewModel : ObservableObject
{
// Partial Property
[ObservableProperty]
public partial string? Title { get; protected set; }
}
// Generated code
// 'field' is a contextual keyword to access the backing field for semi-auto properties
partial class BaseViewModel
{
// Implementation
public partial string? Title
{
get => field;
protected set
{
// Verify if the value has indeed changed
field = value;
// Raise the OnPropertyChanged notification
}
}
}
Generated Code:

Configuration:
The .NET 9 SDK must be installed on the developer’s machine for proper functionality.
Since the C# language feature regarding access to the backing field in semi-auto properties using the contextual keyword
fieldhas not yet been formalized, hence it is necessary to set the LangVersion project property to the valuepreviewfor this Partial Properties feature to function correctly.
For brevity, only the relevant property is shown.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<Project>
Limitation:
These are not just on this particular Toolkit, but on the feature as a whole.
Update: To address this limitation on the CLI build, install the .NET 9 GA or later release.
Semi-auto properties are not functioning in the CLI.- Fails with this error message: The name ‘field’ does not exist in the current context.
- Whereas the same works from the Visual Studio IDE (17.12 Preview 3.0 or later)
- Both VS Code and Rider are yet to support this feature – SG is working but build fails
Should there be any supplementary limitations or possible workarounds pertaining to those mentioned, please feel free to share your insights in the comments.
Code Sample:
A functioning sample of this Partial Properties has been made available in my .NET MAUI Samples GitHub repository. Please refer to the src\NET_8\DateCalculator folder for the multi-project solution.
Specifically, look into the DateViewModel.cs source file located in the ViewModels folder of the DateCalculator.Shared library project.
Have added a C# preprocessor directive symbol named FIELDS. When it’s defined, SG operates based on the fields, and when it’s undefined or commented out, SG functions using the partial properties.
To work with Partial Properties, comment out the FIRST line of code. By default, it uses Fields.
Happy coding. Stay connected as we continue to learn and share the experiences from this exciting journey of being a .NET developer.
One reply on “MVVM Toolkit v8.4: Custom Access Modifiers for Partial Properties – How To”
[…] MVVM Toolkit v8.4: Custom Access Modifiers for Partial Properties – How To (Vijay Anand E G) […]