DataTemplate / ContentTemplate - 交换控制
我该如何解决以下(简化的)问题? MV-VM 上下文。我想在用户界面上显示文本。 如果用户有权更改文本,我想使用文本框来操作文本。 如果用户没有权限,我想使用标签仅显示文本。 我的主要问题:如何交换文本框和标签以及绑定文本。内容为视图模型中的相同属性。
感谢您的回答 托尼
How can i solve the following (simplified) problem?
M-V-VM context. I want to show text at the UI.
In case the user has the rights to change the text, i want to use a textbox to manipulate the text.
In case the user has no rights, i want to use a label to only show the text.
My main problem: how to exchange textbox and label and bind Text resp. Content to the same property in viewmodel.
Thanks for your answers
Toni
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种方法可以实现这一点,并且重用的难易程度各有不同。您可以拥有一个 DataTemplateSelector,它可以为给定属性返回适当的 DataTemplate(根据其编写方式,您可以将它用于每个属性)。
您可以为每个属性创建一个 DataTemplate,并根据 DataTrigger 更改可见性(这真的很烦人,因为需要进行大量复制和粘贴)。
我认为最简单的方法是使用专门的 TextBox ControlTemplate。基本上,当它被禁用时,您可以让它看起来像一个 TextBlock,而不是将其灰显:
然后您可以像这样使用它:
There are a few ways of achieving this, with varying degrees of ease of reuse. You can have a DataTemplateSelector that could return the appropriate DataTemplate for a given property (depending on how this is written, you may be able to use it for each of your properties).
You could create a DataTemplate for each property, and change visibility based on a DataTrigger (this gets really annoying, as it is a lot of copy and paste).
I think the easiest way of doing this is with a specialized ControlTemplate for the TextBox. Basically, when it is disabled, instead of graying it out, you can just make it look like a TextBlock:
Then you can use it like so: