通过 WPF 中的样式设置器将浮点数分配给小数属性

发布于 2024-09-01 12:37:48 字数 630 浏览 0 评论 0原文

我在无外观控件的模板中有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风苍溪 2024-09-08 12:37:49

您可以尝试将其包装在 Binding 中,而不是直接使用 {x:Static ...} 标记扩展:

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

这可能已经像 >Binding 通常负责必要的转换。如果它不起作用,您可以向 Binding 添加适当的 IValueConverter

为了提高性能,您还可以将 Mode 属性设置为 OneTime 而不是 OneWay。但是,我不确定这是否有效。祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文