开发者

Assigning a float to a decimal property via a style setter in WPF

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开发者_JS百科.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?


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜