Skip to content

Add ComboBoxAssist.MaxLength attached property in ComboBox to control MaxLength when IsEditable is set to True #2442

@jamesport079

Description

@jamesport079

ComboBox with IsEditable set to true essentially behaves as a TextBox with an autosuggest dropdown list. A silly ommission in WPF is that MaxLength cannot be set in XAML for an editable ComboBox.

I created an attached property to address this shortcoming. I strongly feel this can be integrated in this library in the form of materialDesign:ComboBoxAssist.MaxLength="___" helper class.

The code to implement it is pretty straightforward, essentially creating an attached property MaxLength and finding the TextBox part in ComboBox and setting MaxLength on TextBox. FindChild does the magic to find the TextBox part of the editable ComboxBox.

private static void OnMaxLenghtChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
    if (obj is ComboBox comboBox)
    {
        comboBox.Loaded +=
          (s, e) =>
          {
              DependencyObject? textBox = comboBox.FindChild<TextBox>("PART_EditableTextBox");
              if (textBox == null)
              {
                  return;
              }

              textBox.SetValue(TextBox.MaxLengthProperty, args.NewValue);
          };
    }
}

If you think this is beyond the scope of this library I would at least suggest to add TextFieldAssist.CharacterCounterVisibility to be accessible in an editable ComboBox for the sake of consistency.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions