本文介绍了如何以一种方式将一个 texblock 值绑定到其他 textblock 并在 MVVM 中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我用一张图片解释我的问题

Let me explain my question by an Image

我的项目中有 MVVM 结构.我有两个文本块,Textblock 1textblock 2.现在我想要 textblock2's Text 与 Textblock1's Text 相同,只要 Textblock1 的 Text 改变.但我应该能够设置 Textblock2'sTextblock1's 不同的文本.所以我设置了Textblock1的Text属性的单向绑定.

I have MVVM structure in My project.I have two textblocks, Textblock 1 and textblock 2. Now I want textblock2's Text same as Textblock1's Text, whenever Textblock1's Text changed.but I should be able to set Textblock2's Text different from Textblock1's.So I am setting Oneway binding of Textblock1's Text Property.

如何在 MVVM 中获取 Textblock2's 的 Text 属性.如果我为 Textblock2's Text 属性创建一个属性,我将无法将 Textblock1's 文本绑定到 textblock2.

How Can I get the Text property of Textblock2's In MVVM. If I create a property for Textblock2's Text property, I wont be able to bind Textblock1's text to textblock2.

如果我想进一步澄清我的问题,请告诉我.

Let me know if I want to clear my question further.

期待中的感谢.

推荐答案

在 VM 中使用两个属性,并在那里实现相等/覆盖逻辑.这正是 VM 擅长的东西.

Use two properties in VM, and implement the equal/override logic there. That's exactly the kind of stuff VMs are good in.

VM 
  Prop1 <-- Binding- TextBlock1
  Prop2 <-- Binding- TextBlock2

实现 Prop1 设置器,以便它也更新 Prop2(不要忘记 INotifyPropertyChanged),如果您设置 Prop2 使其切换到并保持不同的值.

The Prop1 setter is implemented such that it also updates Prop2 (don't forget INotifyPropertyChanged), if you set Prop2 make it to switch to and keep the different value.

这篇关于如何以一种方式将一个 texblock 值绑定到其他 textblock 并在 MVVM 中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 07:19