views:

54

answers:

1

I have the following xaml in a template for a lookless control:

<Style x:Key="NumericUpDownStyle" TargetType="controls:NumericUpDown">
    <Style.Setters>
        <Setter Property="Change" Value="{x:Static local:Preferences.ChangeAmount}"/>
    </Style.Setters>
</Style>

Where the 'Change' property on the 'NumericUpDown' control is a decimal, and the static 'Preferences.ChangeAmount' is a float.

This fails with the following error:

'1' is not a valid value for property 'Change'

Is there a way to get the style to cast the float to a decimal? It is not an option to change the 'NumericUpDown' control, the underlying control that I am templating, or the 'Preferences.ChangeAmount' property. I can make some static wrapper properties somewhere to do the casting but that seems silly to me.

Any ideas?

A: 

Instead of using the {x:Static ...} Markup Extension directly, you could try to wrap it in a Binding like that:

<Setter Property="Change" Value="{Binding Source={x:Static local:Preferences.ChangeAmount}, Mode=OneWay}"/>

This might already work as the Binding typically takes care of necessary conversions. If it does not work, you could add an appropriate IValueConverter to the Binding.

To improve performance you could also set the Mode property to OneTime instead of OneWay. However, I am not sure whether this will work. Good luck!

gehho