让文本框显示基于模型视图中布尔值的消息的最佳方法
我有一个文本框,需要更改其边框颜色,然后在文本框下方显示一条消息。应根据模型中的布尔值显示/隐藏此消息。实现这一目标的最佳方法是什么?
I have a TextBox that needs to have its border change color and then display a message below the text box. This message should be displayed/hidden based on a bool value in the model. What is the best way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有很多不同的方法可以做到这一点。如果您只想执行此操作一次,最简单的方法是将
TextBlock
添加到布局中并使用样式来隐藏它,例如:如果您希望能够重复此操作,您需要将其制作为模板或用户控件。
另请注意,将可见性从折叠更改为可见将更改整体布局,这可能会产生各种不良效果。根据您的设计,您可以将可见性默认设置为隐藏。
There are a ton of different ways of doing this. If you're only going to do this once, the simplest way is to add the
TextBlock
to the layout and use a style to hide it, e.g.:If this is something you want to be able to repeat, you'll want to make this into a template or a user control.
Also, note that changing the visibility from collapsed to visible will change the overall layout, which could have all kinds of undesirable effects. Depending on your design, you might make the visibility default to hidden.
您可以使用
DataTrigger
根据 ViewModel 中的值设置文本框的文本、可见性和外观。这似乎是最简单的解决方案。另一种选择是创建一个 IValueConverter 来转换布尔值以获取文本、可见性和颜色。
You can use a
DataTrigger
to set the text, visibility, and appearance of the textbox based on the value in the ViewModel. This seems like the simplest solution.Another option is to create an
IValueConverter
to convert the bool to get the text, visibility, and color.