I am experiencing an issue with data binding in .NET MAUI when using the [AddINotifyPropertyChangedInterface] attribute from the PropertyChanged.Fody package.
I have a view model that implements the INotifyPropertyChanged interface using this attribute, and I have set up data bindings between my view model and my view.
The issue Iām experiencing is that the two way data bindings for the CheckBox and Switch controls are not working correctly.
When I interact with these controls directly (e.g., by toggling the Switch or checking/unchecking the CheckBox),then onwards the view model property is not updating.
However, when I update the view model property programmatically (e.g., by clicking a Button that executes a Command), the controls do update correctly till I interact with controls directly.
Thanks & Regards Vijith
Here is an example of my code:
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace MauiApp4.MVVM.ViewModels
{
[AddINotifyPropertyChangedInterface]
internal class MainViewModel
{
public ICommand Command => new Command(command);
public bool isSelected { get; set; }
void command()
{
isSelected = !isSelected;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp4.MainPage">
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Label Text="{Binding isSelected}"/>
<CheckBox IsChecked="{Binding isSelected}"/>
<Switch IsToggled="{Binding isSelected}"/>
<Button Text="Change" Command="{Binding Command}"/>
</VerticalStackLayout>
</ScrollView>
</ContentPage>
using MauiApp4.MVVM.ViewModels;
namespace MauiApp4
{
public partial class MainPage : ContentPage
{
int count = 0;
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}
}
}