两路多功能转换器或具有属性的转换器

发布于 2024-12-17 19:46:30 字数 1303 浏览 2 评论 0原文

我需要将两部分值转换为字符串并再次转换回来,例如:

{Value = 12.0, Units = DimensionUnits.Inches}

转换为

"12 in"

使用多值转换器从源进行转换非常简单,但如果用户未在字符串中提供单位类型,则无法转换回来所以多值转换器看起来不像我需要的解决方案。

直接转换器参数不起作用,因为需要绑定单位类型,因此我研究了如何创建可绑定参数。创建可绑定参数实际上非常简单 - 从 DependencyObject 派生 - 但你会遇到转换器不在可视化树中的问题 - 因此无法绑定到任何东西 - 有两种解决方案:

http://tomlev2.wordpress.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/ http://shujaatsiddiqi.blogspot.com/2011/02/wpf -binding-converter-parameter.html

第一种方法从 Freezable 而不是 DependencyObject 派生转换器,以允许 DependencyProperties。这可以工作并允许您在Whatever.Resources 部分中进行绑定,但它具有极其奇怪的行为,例如仅在第一次在整个应用程序中使用绑定时侦听绑定。

第二种方法似乎根本不起作用。当源更改时,依赖属性永远不会更新。

<pf:BindingReflector Target="{Binding Source={StaticResource DistanceConverter}, Path=Units, Mode=TwoWay}"
    Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=g:TestWindow, AncestorLevel=1}, Path=Units, Mode=TwoWay}"/>

有谁知道这个问题的解决方案或者这是 WPF 的一个很大的限制吗?

I need to convert a two part value into a string and back again for example:

{Value = 12.0, Units = DimensionUnits.Inches}

Converts to

"12 in"

This is pretty simple using a multivalue converter to convert from source but becomes impossible to convert back if the user doesn't provide the unit type in the string so a Multivalue converter doesn't look like the solution I need.

A direct converter parameter won't work because the unit type needs to be bound so I researched how to create a bindable parameter. Creating a bindable parameter is actually pretty easy - deriving from DependencyObject - but then you have the problem of your converter not living in the visual tree - and thus not being able to bind to anything - to which there are 2 solutions:

http://tomlev2.wordpress.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
http://shujaatsiddiqi.blogspot.com/2011/02/wpf-binding-converter-parameter.html

The first method derives your Converter from Freezable instead of DependencyObject to allow DependencyProperties. This works and allows you to bind within the Whatever.Resources section but it has extremely odd behavior like only listening to the binding the first time it is used in your entire application.

The second method doesn't seem to work at all. The dependency property is never updated when the source changes.

<pf:BindingReflector Target="{Binding Source={StaticResource DistanceConverter}, Path=Units, Mode=TwoWay}"
    Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=g:TestWindow, AncestorLevel=1}, Path=Units, Mode=TwoWay}"/>

Does anyone know of a solution to this problem or is this a big limitation of WPF?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

筱武穆 2024-12-24 19:46:30

就我个人而言,我实际上建议重新考虑一下这一点,并执行以下任一操作:

  1. 使用IMultiValueConverter单向保留最终字符串,并将其作为两个单独的项目输入。这似乎是一种合理的方法,因为单位似乎是具有一组特定选项的枚举。单位的组合框和金额的文本框似乎很合适,并且可以使用单向转换器完成总显示。

  2. 在 ViewModel 中显式处理此转换。这样做的优点是允许更好的验证处理,这可能是需要的,因为在一个控件中输入两个单独的值(金额+单位)可能无法正确验证。通过将逻辑直接移动到 ViewModel 中而不是绑定到属性,您可以以干净的方式正确处理错误。

Personally, I would actually suggest rethinking this a bit, and doing either:

  1. Keep the final string one-way using a IMultiValueConverter, and have this be input as two separate items. This seems like a reasonable approach, as the units appear to be an enum with a specific set of options. A combo box for units and textbox for amount seems appropriate, and the total display can be done with a one-way converter.

  2. Handle this conversion explicitly in the ViewModel. This has the advantage of allowing much better validation handling, which is probably going to be required as entering two separate values (amount + units) in one control is likely to not validate correctly. By moving the logic directly into your ViewModel instead of binding to the properties, you can correctly handle errors in a clean way.

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