how to bind a boolean to combobox in wpf

白昼怎懂夜的黑 提交于 2019-12-17 19:35:06

问题


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


回答1:


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}}"/>



回答2:


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>



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/4335339/how-to-bind-a-boolean-to-combobox-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!