2016-10-06 11 views
2

레이블의 Visibility 속성은 BoolToVisiblityConverter를 사용하여 RadioButton의 IsChecked 속성에 연결할 수 있습니다. 그러나 한 레이블의 가시성을 둘 이상의 IsChecked 속성에 연결하는 방법은 무엇입니까? RadioButtons를 2 또는 4가 선택되는 경우 RadioButtons를 1, 3 또는 5 true, <Label Content="2"></Label>의 IsChecked로 설정 한 예를 들어 <Label Content="1"></Label> 레이블의 Visibility 속성을 여러 RadioButton에 바인딩하는 방법은 무엇입니까?

이 표시되어야한다.

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition></RowDefinition> 
     <RowDefinition></RowDefinition> 
    </Grid.RowDefinitions> 
    <StackPanel> 
     <RadioButton Content="1"></RadioButton> 
     <RadioButton Content="2"></RadioButton> 
     <RadioButton Content="3"></RadioButton> 
     <RadioButton Content="4"></RadioButton> 
     <RadioButton Content="5"></RadioButton>  
    </StackPanel> 
    <StackPanel Grid.Row="1"> 
     <Label Content="1"></Label> 
     <Label Content="2"></Label> 
    </StackPanel> 
</Grid> 
+0

가 응답하도록 돕는다 : 완전히 XAML에서 컨버터를 사용하지 않고? – Spontifixus

+0

완료 코드 뒤에 값 추가가 없습니다. – GauravKP

+3

[레이블에 대한 다중 RadioButton 가시성 바인딩]의 가능한 복제본 (http://stackoverflow.com/questions/11696924/multiple-radiobutton-visibility-binding-for-a-label) – Spontifixus

답변

2

당신이 또는 코드 숨김에 의존 할 (당신이보기 모델 제어 UI를 의미)를 MVVM 접근 방식을 사용하고 있습니까 :

<StackPanel> 
       <RadioButton x:Name="RadioButton1" Content="1" /> 
       <RadioButton x:Name="RadioButton2" Content="2" /> 
       <RadioButton x:Name="RadioButton3" Content="3" /> 
       <RadioButton x:Name="RadioButton4" Content="4" /> 
       <RadioButton x:Name="RadioButton5" Content="5" /> 
</StackPanel> 
<StackPanel Grid.Row="1"> 
       <Label x:Name="FirstLabel" Content="1"> 
        <Label.Style> 
         <Style> 
          <Style.Triggers> 
           <MultiDataTrigger> 
            <MultiDataTrigger.Conditions> 
             <Condition Binding="{Binding ElementName=RadioButton1, Path=IsChecked}" Value="True" /> 
             <Condition Binding="{Binding ElementName=RadioButton3, Path=IsChecked}" Value="True" /> 
             <Condition Binding="{Binding ElementName=RadioButton5, Path=IsChecked}" Value="True" /> 
            </MultiDataTrigger.Conditions> 
            <MultiDataTrigger.Setters> 
             <Setter TargetName="FirstLabel" Property="Visibility" Value="Visible" /> 
            </MultiDataTrigger.Setters> 
           </MultiDataTrigger> 
          </Style.Triggers> 
         </Style> 
        </Label.Style> 
       </Label> 
      </StackPanel> 
+0

좋은 제안입니다. 그러나 AND 관계를 구현하는 조건. 내 경우에는 implemet OR Relation이 필요하다. – A191919

+0

그런 다음 각 조건에 대해 간단한 DataTrigger 만 추가하십시오. – lhildebrandt

+0

와우, 아주 영리한 솔루션입니다! – bakala12

1

당신은 RadioButton의 1,3,5 하나는이 방법을 시도 할 수 체크 할 때 Label이 볼 수 있도록하려는 경우. 다중 바인딩 및 변환기를 정의하십시오. 변환기 :

public class LabelVisibilityConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (values == null) return false; 
     return values.Any(v => 
     { 
      bool? b = v as bool?; 
      return b.HasValue && b.Value; 
     }); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

이 예입니다. Convert 메서드에서 values ​​매개 변수의 값이 bool이고 값이 true인지 여부를 확인합니다. 여기에 마크 업 컨버터를 사용하는 방법은 다음과 같습니다

<Grid> 
<Grid.Resources> 
    <conv:LabelVisibilityConverter x:Key="LabelConverter"/> 
</Grid.Resources> 
<Grid.RowDefinitions> 
    <RowDefinition></RowDefinition> 
    <RowDefinition></RowDefinition> 
</Grid.RowDefinitions> 
<StackPanel> 
    <RadioButton x:Name="1" Content="1"></RadioButton> 
    <RadioButton x:Name="2" Content="2"></RadioButton> 
    <RadioButton x:Name="3" Content="3"></RadioButton> 
    <RadioButton x:Name="4" Content="4"></RadioButton> 
    <RadioButton x:Name="5" Content="5"></RadioButton>  
</StackPanel> 
<StackPanel Grid.Row="1"> 
    <Label Content="1"> 
     <Label.Visibility> 
      <MultiBinding Converter="{StaticResource LabelConverter}"> 
       <Binding Path="IsChecked" ElementName="1"/> 
       <Binding Path="IsChecked" ElementName="3"/> 
       <Binding Path="IsChecked" ElementName="5"/> 
      </MultiBinding> 
     </Label.Visibility> 
    </Label> 
    <Label Content="2"> 
     <Label.Visibility> 
      <MultiBinding Converter="{StaticResource LabelConverter}"> 
       <Binding Path="IsChecked" ElementName="2"/> 
       <Binding Path="IsChecked" ElementName="4"/> 
      </MultiBinding> 
     </Label.Visibility>   
    </Label> 
</StackPanel> 
</Grid> 

로 변환 (전환 접두사)의 네임 스페이스를 매핑하는 것을 잊지 마십시오 당신은 값을 설정하는 MultiDataTrigger를 사용할 수 있습니다

xmlns:conv="clr-namespace:..." 
관련 문제