通过 WPF 中的样式设置器将浮点数分配给小数属性
我在无外观控件的模板中有以下 xaml:
<Style x:Key="NumericUpDownStyle" TargetType="controls:NumericUpDown">
<Style.Setters>
<Setter Property="Change" Value="{x:Static local:Preferences.ChangeAmount}"/>
</Style.Setters>
</Style>
其中 NumericUpDown
控件上的 Change
属性是十进制,静态 Preferences.ChangeAmount
code> 是一个浮点数。
此操作失败并出现以下错误:
“1”不是属性“Change”的有效值
有没有办法让样式将浮点数转换为小数?它不是更改 NumericUpDown 控件、我正在模板化的基础控件或 Preferences.ChangeAmount 属性的选项。我可以在某处创建一些静态包装属性来进行转换,但这对我来说似乎很愚蠢。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试将其包装在
Binding
中,而不是直接使用{x:Static ...}
标记扩展:这可能已经像
>Binding
通常负责必要的转换。如果它不起作用,您可以向Binding
添加适当的IValueConverter
。为了提高性能,您还可以将
Mode
属性设置为OneTime
而不是OneWay
。但是,我不确定这是否有效。祝你好运!Instead of using the
{x:Static ...}
Markup Extension directly, you could try to wrap it in aBinding
like that:This might already work as the
Binding
typically takes care of necessary conversions. If it does not work, you could add an appropriateIValueConverter
to theBinding
.To improve performance you could also set the
Mode
property toOneTime
instead ofOneWay
. However, I am not sure whether this will work. Good luck!