Reputation: 3462
The Visibility-property of a Label can be connected to the IsChecked-property of a RadioButton using a BoolToVisiblityConverter. But how to connect one label's visibility to more than one IsChecked-property?
For Example <Label Content="1"></Label>
should be visible when RadioButtons 1, 3 or 5 have IsChecked set to true
, <Label Content="2"></Label>
when RadioButtons 2 or 4 are checked.
<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>
Upvotes: 2
Views: 746
Reputation: 334
You can use a MultiDataTrigger to set the value entirely in Xaml without the use of a converter:
<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>
Upvotes: 2
Reputation: 378
When you want your Label
to be visible when one from RadioButton
s 1,3,5 is checked you can try this way.
Define a multibinding and a converter.
Converter:
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();
}
}
This is an example. In Convert method we check whether any from values parameter is bool and has value true. Here is how to use the converter in markup:
<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>
Don't forget to map a namespace of your converter (conv prefix) with:
xmlns:conv="clr-namespace:..."
Upvotes: 1