gpt4 book ai didi

.net - 根据选择的单选按钮显示控件

转载 作者:行者123 更新时间:2023-12-04 16:30:36 25 4
gpt4 key购买 nike

我有一组三个单选按钮。根据选择的单选按钮,我想显示三个控件之一 - 文本框、下拉列表或按钮。如何根据选定单选按钮的结果显示控件?

最佳答案

您可以使用 BooleanToVisibilityConverter 将控件的可见性绑定(bind)到 RadioButton 的 IsChecked 属性。 :

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<BooleanToVisibilityConverter x:Key="convVisibility"/>
</Page.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<RadioButton Name="radioButton1" GroupName="group1">Control1</RadioButton>
<RadioButton Name="radioButton2" GroupName="group1">Control2</RadioButton>
<RadioButton Name="radioButton3" GroupName="group1">Control3</RadioButton>
<Grid>
<Button Visibility="{Binding IsChecked, ElementName=radioButton1, Converter={StaticResource convVisibility}}">1. Button</Button>
<TextBlock Visibility="{Binding IsChecked, ElementName=radioButton2, Converter={StaticResource convVisibility}}">2. TextBlock</TextBlock>
<TextBox Visibility="{Binding IsChecked, ElementName=radioButton3, Converter={StaticResource convVisibility}}">3. TextBox</TextBox>
</Grid>
</StackPanel>
</Grid>
</Page>

编辑 :

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 设计器中,控件永远不会隐藏......

无论如何,您可以实现自己的转换器,它总是在设计模式下返回 Visible。由于某种晦涩的原因, BooleanToVisibilityConverter 类是密封的,因此您不能从它继承。如果您不想重写转换逻辑,则可以将转换委托(delegate)给 BooleanToVisibilityConverter :
public class MyBooleanToVisibilityConverter : IValueConverter
{
private BooleanToVisibilityConverter _converter = new BooleanToVisibilityConverter();
private DependencyObject _dummy = new DependencyObject();

private bool DesignMode
{
get
{
return DesignerProperties.GetIsInDesignMode(_dummy);
}
}

#region IValueConverter Members

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (DesignMode)
return Visibility.Visible;
else
return _converter.Convert(value, targetType, parameter, culture);
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return _converter.ConvertBack(value, targetType, parameter, culture);
}

#endregion
}

关于.net - 根据选择的单选按钮显示控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1044772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com