Silverlight 转换器,用于将对象转换为字符串并返回

发布于 2024-11-13 07:12:20 字数 364 浏览 7 评论 0原文

我的模型中有一个 Address 对象,它有 6 个地址行作为单独的属性。在我的 Silverlight 视图中,我希望将其显示在多行文本框中并使用数据绑定进行更新。

View 链接到 ViewModel,该 ViewModel 具有始终设置的 Address 属性。我的第一个想法是使用 ValueConverter,它最初很好,因为它可以接受一个 Address 对象并传回一个显示的字符串。

当我想转换回字符串时,我的问题出现了。我希望更新现有的 Address 对象,但找不到方法来执行此操作。我认为你可以传回一个新的 Address 对象,但这不是我想要发生的事情。

我在这里走在正确的轨道上,或者有没有比使用 ValueConverter 更好的方法。

I have an Address object in my model which has 6 address lines as separate properties. In my Silverlight view I want this to be displayed in a multiline text box and to be updated using databinding.

The View is linked to a ViewModel which has an Address property which is always set. My first thought was to use a ValueConverter which was initially fine as it could take an Address object and pass back a string which is displayed.

My problem arises when I want to ConvertBack the string. I wish to update the existing Address object but cannot find a way to do this. I think you can pass back a new Address object but this is not what I want to happen.

I am on the right track here or is there a better method than using a ValueConverter.

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

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

发布评论

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

评论(1

最近可好 2024-11-20 07:12:20

您几乎已经走在正确的轨道上了!您的观察是正确的,ConvertBack 方法需要创建一个新的 Address 实例。您无法在转换器中获得对原始地址的引用,绑定框架不允许这种紧密耦合。

使用 MVVM 怎么样?创建一个 ViewModel,将您的地址公开/调整为字符串,基本上执行您的值转换器的角色。当 TwoWay 绑定更新此字符串属性时,您的 ViewModel 可以更新它所适应的地址。

You are almost on the right track here! You are right in your observation that the ConvertBack method would need to create a new Address instance. You cannot obtain a reference back to the original address in your converter, the binding framework does not permit this kind of tight coupling.

How about using MVVM? create a ViewModel that exposes / adapts your Address as a string, basically performing the role of your value converter. When the TwoWay binding updates this string property, your ViewModel can then update the Address that it adapts.

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