In WinUI, I want to create a Decimal NumberBox (like a regular number box, but accepting and returning decimals instead of doubles) and base the style off the existing NumberBox control. How do I do this?
This is what the code behind the control looks like:
public class DecimalBox : NumberBox
{
public DecimalBox()
{
this.DefaultStyleKey = typeof(DecimalBox);
}
public new decimal Value
{
get { return Convert.ToDecimal(base.Value); }
set { base.Value = Convert.ToDouble(value); }
}
}
And this is how I tried to style it:
<Style TargetType="controls:DecimalBox" BasedOn="{StaticResource DefaultNumberBoxStyle}"/>
But I don't know what to put in for the existing DefaultNumberBoxStyle. There is no "DefaultNumberBoxStyle" in the default generic resource dictionary. In the old days, we'd use something like:
<Style TargetType="controls:DecimalBox" BasedOn="{StaticResource {x:Type NumberBox}}"/>
But I don't know what the analog is in WinUI.
If you just want to use the default NumberBox
template, you can do it this way:
public sealed class DecimalBox : NumberBox
{
public DecimalBox()
{
this.DefaultStyleKey = typeof(NumberBox);
}
public new decimal Value
{
get => Convert.ToDecimal(base.Value);
set => base.Value = Convert.ToDouble(value);
}
}
If you want to modify the template, you need to bring the entire template from generic.xaml since there's no "DefaultNumberBoxStyle".