【问题标题】:how to bind a boolean to combobox in wpf如何将布尔值绑定到wpf中的组合框
【发布时间】:2014-10-13 16:22:54
【问题描述】:

我想知道如何将布尔属性绑定到组合框。组合框将是一个是/否组合框。

【问题讨论】:

    标签: wpf binding


    【解决方案1】:

    您可以使用 ValueConverter 将布尔值转换为 ComboBox 索引并返回。像这样:

    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;
            }
        }
    }
    

    假设 Yes 在索引 0 上,No 在索引 1 上。那么您必须使用该转换器绑定到 SelectedIndex 属性。为此,您在资源部分声明您的转换器:

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

    然后你在绑定中使用它:

    <ComboBox SelectedIndex="{Binding YourBooleanProperty, Converter={StaticResource boolToIndexConverter}}"/>
    

    【讨论】:

    • 很好的答案。此link 提供了有关该主题的更多信息。
    【解决方案2】:

    我发现自己过去曾为此使用 ComboBox 项的 IsSelected 属性。此方法完全在 xaml 中。

    <ComboBox>
        <ComboBoxItem Content="No" />
        <ComboBoxItem Content="Yes" IsSelected="{Binding YourBooleanProperty, Mode=OneWayToSource}" />
    </ComboBox>
    

    【讨论】:

    • 这工作得很好,但第一次 ComboBox 没有选择任何值。您可以通过添加SelectedIndex 来强制使用默认值,例如&lt;ComboBox SelectedIndex="0"&gt;
    • 您可以在“否”框上放置一个转换器来反转 IsSelected 布尔绑定,然后它应该只从您的视图模型中获取默认值。
    【解决方案3】:

    第一个解决方案是用复选框替换“是/否”组合框,因为复选框的存在是有原因的。

    第二种解决方案是用真假对象填充组合框,然后将组合框的“SelectedItem”绑定到布尔属性。

    【讨论】:

    • 大声笑不确定我是否会采用您的解决方案,但 +1 因为我什至没有想到。
    【解决方案4】:

    这是一个示例(将启用/禁用替换为是/否):

    <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>
    

    这里是转换器:

    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);
        }
    }
    

    而且无需使用索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-07
      • 2012-11-12
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      相关资源
      最近更新 更多