Search code examples
mvvmuwpwindows-community-toolkit

How to animate Community Toolkits RadialGauge with MVVM?


I would like to animate the radialgauge of the Windows Community Toolkit but unfortunately the UI does not refresh itself after a RaisePropertyChanged.

enter image description here

Here is my Code:

View

<TextBlock Grid.Row="0" Text="{Binding RadialGaugeValue}" />
<controls:RadialGauge 
    x:Name="MyRadialGauge"
    Grid.Row="01"
    Value="{Binding RadialGaugeValue}"
    Minimum="0"
    Maximum="100"
    StepSize="1"
    IsInteractive="False"
    TickSpacing="10"
    TickWidth="0"
    ScaleWidth="4"
    ScalePadding="29"
    ScaleTickWidth="1" 
    MinAngle="245"
    MaxAngle="115"
    Unit="Sparquote"
    NeedleWidth="2"
    NeedleLength="100" 
    MaxHeight="250" 
    Margin="0,20,0,0" 
    NeedleBrush="{ThemeResource SystemColorHighlightBrush}" 
    FocusVisualPrimaryBrush="{x:Null}"
    FocusVisualSecondaryBrush="Green"
    TrailBrush="{ThemeResource SystemColorHotlightBrush}" 
    ValueStringFormat=".&quot;%&quot;"> 

ViewModel

private async Task DoSavingRateAnimation(double myValue)
{
    var progress = new Progress<int>(value =>
    {
        RadialGaugeValue = value;
        RaisePropertyChanged("RadialGaugeValue");
    });
    await SavingRateAnimation(progress, myValue);

}

private Task SavingRateAnimation(IProgress<int> progress, double value)
{
    return Task.Run(() =>
    {
        int i = 0;
        while (RadialGaugeValue < value)
        {
            Task.Delay(100).Wait();
            i += 1;
            progress.Report(i);
        }
    });
}

As you can see, the radialGauge does not update itself.

Do you have any idea how I could animate the Control?


Solution

  • Please set RadialGauge value binding with two way model just like the following.

    Value="{Binding RadialGaugeValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"