Search code examples
wpfxamldata-bindingmultibinding

Binding of label to two radio buttons not working- WPF


I have 2 radio buttons and label. I wish that visibility property of the label will be "visible" only in case one of the radio buttons was checked.

On MainWindow.xaml:

<Label x:Name="outputFolderLabel" Content="Select destination folder:"     
Height="30" Grid.Row="1" Grid.Column="0" FontSize="13.333" Margin="5 10">
  <Label.Visibility>
      <MultiBinding Converter="{StaticResource FilterConverter}">
          <Binding ElementName ="RadioButNew" Path="IsChecked" Mode="OneWay"/>
          <Binding ElementName ="RadioButUpdate" Path="IsChecked" Mode="OneWay"/>
      </MultiBinding>
   </Label.Visibility>
</Label>

On MainWindow.xaml.cs:

public class SearchFilterConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (bool)values[0] || (bool)values[1];
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

And it doesn't work.. any suggestions please? I'm newbie on WPF..

Thanks!


Solution

  • You are returning a bool from your converter. Instead of you have to return a System.Windows.Visibility.

    es:

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
       if (values[0] is bool && values[1] is bool)
         if ((bool)values[0] || (bool)values[1])
           return Visibility.Visible;
    
       return Visibility.Collapsed;
    }