선택한 라디오 버튼을 기반으로 컨트롤 표시
-
20-08-2019 - |
문제
3 개의 라디오 버튼 그룹이 있습니다. 어떤 라디오 버튼을 선택했는지에 따라 텍스트 상자, 드롭 다운 목록 또는 버튼의 세 가지 컨트롤 중 하나를 혼란스럽게 만들고 싶습니다. 선택한 라디오 버튼의 결과를 기반으로 컨트롤을 표시하려면 어떻게해야합니까?
해결책
당신은 제어의 가시성을 BooleanToVisibilityConverter
:
<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>
편집하다 :
그 솔루션은 훌륭하게 작동하며 구현하기가 간단합니다. 어쨌든 디자인 모드에서 컨트롤이 숨겨져있는 것을 방지 할 수 있습니까?
나는 다른 디자이너에 대해 잘 모르지만 (예 : Blend), Visual Studio Designer에서는 컨트롤이 숨겨져 있지 않습니다 ...
어쨌든, 당신은 자신의 컨버터를 구현할 수 있으며, 이는 항상 디자인 모드에서 보이는 반환입니다. 몇 가지 모호한 이유로 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
}
다른 팁
설계 시간 동안 구성 요소를 보이려면 추가 할 수 있습니다. FallbackValue
의 true
확인란에 :
<RadioButton x:Name="cbxEmail" Content="Email Details" IsEnabled="{Binding IsEmail, FallbackValue=true}" IsChecked="{Binding IsEmail, Mode=OneWay, FallbackValue=true}"
Grid.Column="2"/>
제휴하지 않습니다 StackOverflow