Search code examples
c#.netxamlmvvmwinui-3

Use ProgressRing In MVVM WinUI 3


I have a ProgressRing in my xaml that on processing my command, it will rotate:

<ProgressRing Name="progressRing" IsActive="{Binding ProgressActive}" />

I have a bool Property as the name in the binding shows. at the first line of my command, I enable (True) this value and wanted to see the progress ring. I tested it on debug and the value is changing, but nothing on the display. I used Mode OneWay and TwoWay.

If I set a default value for my binding property, It will work if I run, but nothing if application is running and wanted to change it with ViewModel and Command.

Like this:

public bool ProgressActive { get; set; } = true;

Thanks


Solution

  • You need to notify the UI that the property has changed by implementing INotifyPropertyChanged. I always use the CommunityToolkit.Mvvm NuGet package for this.

    The CommunityToolkit.Mvvm NuGet package comes with source generators that generate INotifyPropertyChanged related code for you.

    // This class needs to be "partial" for the CommunityToolkit.Mvvm.
    public partial class ViewModel : ObservableObject
    {
        // This will create an UI interactive "ProgressActive" property for you.
        [ObservableProperty]
        private bool _progressActive;
    }