How to implement NumberFormatter with UWP NumberBox

Viewed 551

I have a NumberBox control in my UWP project.

I want to use it to display a number of minutes, so I want the number on the box to be followed by "min" (e.g. "10 min").

I thought I could implement this using a NumberFormatter, but don't know how.

This is what I tried:

  1. Added NumberFormatter class
public class DurationFormatter : INumberFormatter2
{
    public string FormatInt(long value)
    {
        return $"{value} min";
    }

    public string FormatDouble(double value)
    {
        return $"{value} min";
    }

    public string FormatUInt(ulong value)
    {
        return $"{value} min";
    }
}
  1. Added reference to page resources
<Page.Resources>
    <local1:DurationFormatter x:Key="Converter1"/>
</Page.Resources>
  1. Tried to add formatter to number box, but not sure how. This doesn't work:
<muxc:NumberBox x:Name="DurationNumberBox" SpinButtonPlacementMode="Compact" 
                Header="Duration" Value="{x:Bind DurationViewValue, Mode=TwoWay}" 
                ValidationMode="InvalidInputOverwritten" 
                NumberFormatter="{StaticResource Converter1}"/>

Any help appreciated.

1 Answers
Related