how to bind a boolean to combobox in wpf

前端 未结 4 2007
傲寒
傲寒 2020-12-06 10:26

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

4条回答
  •  囚心锁ツ
    2020-12-06 11:05

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

    
        
            
                
            
        
        
            True
            False
        
    
    

    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.

提交回复
热议问题