15

Well I was wondering how to bind a boolean property to a combobox.Combobox will be a yes/no combobox.

user434547
  • 173
  • 1
  • 1
  • 5

5 Answers5

24

You could use a ValueConverter to convert the boolean value to a ComboBox index and back. Like this:

public class BoolToIndexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((bool)value == true) ? 0 : 1;   
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((int)value == 0) ? true : false;
        }
    }
}

Assuming Yes is on index 0 and No on index 1. Then you'd have to use that converter in binding to the SelectedIndex property. For this, you declare your converter in your resources section:

  <Window.Resources>
    <local:BoolToIndexConverter x:Key="boolToIndexConverter" />
  </Window.Resources>

Then you use it in your binding:

<ComboBox SelectedIndex="{Binding YourBooleanProperty, Converter={StaticResource boolToIndexConverter}}"/>
Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • Great answer. This [link](http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converter(v=vs.110).aspx) provides more information on the subject. – estebro Sep 26 '14 at 19:11
15

I have found myself using the IsSelected property of the ComboBox items for this in the past. This method is entirely in xaml.

<ComboBox>
    <ComboBoxItem Content="No" />
    <ComboBoxItem Content="Yes" IsSelected="{Binding YourBooleanProperty, Mode=OneWayToSource}" />
</ComboBox>
Derrick Moeller
  • 4,808
  • 2
  • 22
  • 48
  • 1
    This works just perfectly, but the first time the ComboBox does not have any value selected. You might force the default value by adding the `SelectedIndex`, for example `` – chviLadislav Aug 21 '18 at 06:43
  • 1
    You could put a converter on the "No" box to invert the IsSelected boolean binding, then it should just take the default from your viewmodel. – Denise Skidmore Oct 05 '18 at 23:32
11

First solution is to replace your 'Yes/No' combobox with a checkbox because, well, checkbox exists for a reason.

Second solution is to fill your combobox with true and false objects and then bind the 'SelectedItem' of your combobox to your Boolean property.

Singleton
  • 3,701
  • 3
  • 24
  • 37
Nicolas Repiquet
  • 9,097
  • 2
  • 31
  • 53
  • lol not sure if I'm going with your solution but +1 because it didn't even occur to me. – Jeff Aug 06 '13 at 19:36
3

Here is an example (replace enabled/disabled with yes/no):

<ComboBox SelectedValue="{Binding IsEnabled}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={x:Static converters:EnabledDisabledToBooleanConverter.Instance}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <ComboBox.Items>
        <system:Boolean>True</system:Boolean>
        <system:Boolean>False</system:Boolean>
    </ComboBox.Items>
</ComboBox>

Here is Converter:

public class EnabledDisabledToBooleanConverter : IValueConverter
{
    private const string EnabledText = "Enabled";
    private const string DisabledText = "Disabled";
    public static readonly EnabledDisabledToBooleanConverter Instance = new EnabledDisabledToBooleanConverter();

    private EnabledDisabledToBooleanConverter()
    {
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Equals(true, value)
            ? EnabledText
            : DisabledText;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //Actually won't be used, but in case you need that
        return Equals(value, EnabledText);
    }
}

And no need to play with indices.

Artiom
  • 7,694
  • 3
  • 38
  • 45
0

You can utilize the ComboBoxItem.Tag property for that:

<ComboBox SelectedValue="{Binding BooleanProperty}" SelectedValuePath="Tag">
   <ComboBoxItem Tag="False">No</ComboBoxItem>
   <ComboBoxItem Tag="True">Yes</ComboBoxItem>
</ComboBox>

Works nicely with .NET 6 (will probably also work for older versions).

David Liebeherr
  • 412
  • 6
  • 8