Displaying controls based on radio button selected(根据选择的单选按钮显示控件)
问题描述
我有一组三个单选按钮.根据选择的单选按钮,我想显示三个控件之一 - 文本框、下拉列表或按钮.如何根据选定单选按钮的结果显示控件?
I have a group of three radio buttons. Depending on which radio button is selected, I want to disaply one of three controls - a textbox, a dropdown list, or a button. How do I display controls based on the result of a selected radio button?
推荐答案
您可以使用 BooleanToVisibilityConverter
将控件的可见性绑定到 RadioButton 的 IsChecked 属性:
You can bind the visibility of the control to the IsChecked property of the RadioButton, using the BooleanToVisibilityConverter
:
该解决方案效果很好,并且易于实施.无论如何我可以防止控件在设计模式下被隐藏?
That solutions works great and it's simple to implement. Is there anyway I can prevent the controls from being hidden in design mode?
我不了解其他设计器(例如 Blend),但在 Visual Studio 设计器中,控件从不隐藏...
I don't know about other designers (Blend for instance), but in the Visual Studio designer the controls are never hidden...
无论如何,您可以实现自己的转换器,该转换器在设计模式下始终返回 Visible.由于某种晦涩的原因, BooleanToVisibilityConverter 类是密封的,因此您不能从它继承.如果您不想重写转换逻辑,则可以将转换委托给 BooleanToVisibilityConverter:
Anyway, you could implement your own converter, which would always return Visible in design mode. For some obscure reason the BooleanToVisibilityConverter class is sealed, so you can't inherit from it. You can delegate the conversion to a BooleanToVisibilityConverter instead, if you don't want to rewrite the conversion logic :
这篇关于根据选择的单选按钮显示控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!