0

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

        }

        
    }

}







1 Answers1

0

I installed PropertyChanged.Fody package and tested the code you provided, but it works well:

When I click CheckBox or Switch, the other two(label and CheckBox/Switch) will change state together. I also add a breakpoint to isSelected property of MainViewModel for debugging, and the result is that isSelected will update.

In addition, there is another way to achieve it:

NuGet install CommunityToolkit.Mvvm package. Then change MainViewModel to this:

public partial class MainViewModel : ObservableObject
{
    [ObservableProperty]
    public bool isSelected;
}

And change isSelected to IsSelected in xaml:

....
 <Label Text="{Binding IsSelected}"/>
 <CheckBox IsChecked="{Binding IsSelected}"/>
 <Switch IsToggled="{Binding IsSelected}"/>    
....

Update:

According to your reply, the cause of the problem is the version of VS. After uninstalling VS 2022 Preview and using VS 2022 instead, the issue has been resolved.

Jianwei Sun - MSFT
  • 2,289
  • 1
  • 3
  • 7