Windows Forms ComboBox - case-insensitive case-sensitive binding

If I bind a winforms combo box, is there a way to make the binding register insensitive?

For example, if a combo box is bound to a property whose value is FOO, set it to select a combo box with a value of Foo?

+3
source share
2 answers

No, It is Immpossible. This is internally implemented using case sensitive reflection.

+4
source

A bit late in the game, but here is what I did to allow case insensitive WinForms ComboBox registration:

, ComboBox, , ( VB.NET):

public object Value {
    get {
        if (string.IsNullOrEmpty(ValueMember)) {
            return Text;
        } else {
            return SelectedValue;
        }
    }
    set {
        if (DesignMode)
            return;

        // If we're databound, Value is the SelectedValue.  Otherwise, it the Text.
        object oldValue = string.IsNullOrEmpty(ValueMember) ? Text : SelectedValue;

        // Want to make sure we're comparing apples to apples, and not specific instances of apples.
        string strOld = oldValue == null ? string.Empty : Convert.ToString(oldValue);
        string strNew = value == null ? string.Empty : Convert.ToString(value);

        if (!string.Equals(strOld, strNew, StringComparison.OrdinalIgnoreCase)) {
            if (ValueMember.HasValue) {
                if (value != null && !string.IsNullOrEmpty(Convert.ToString(value))) {
                    SelectedItem = Items.OfType<object>.FirstOrDefault((System.Object i) => string.Equals(Convert.ToString(FilterItemOnProperty(i, ValueMember)), strNew, StringComparison.OrdinalIgnoreCase));
                } else {
                    SelectedIndex = -1;
                }
            } else {
                Text = value != null ? value.ToString : string.Empty;
            }
            ValidateField();
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs("Value"));
            }
        }
    }
}

ValidateField - , , , , INotifyPropertyChanged Value.

+2

Source: https://habr.com/ru/post/1764571/


All Articles