Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaxLength on editable combobox stops working when mahapps is referenced #1948

Closed
Chrullbo opened this issue Jun 1, 2015 · 6 comments
Closed

Comments

@Chrullbo
Copy link

Chrullbo commented Jun 1, 2015

We have set maxlength on a editable combobox by dependency property and when mahapps is referenced the maxlenght is ignored. We use the string "PART_EditableTextBox" to find the textbox in the visualtree of the combobox that is revealed when editing is enabled

@punker76
Copy link
Member

punker76 commented Jun 1, 2015

@Chrullbo i figured it out, that the loaded stuff should be respect

so this works for me™ (and maybe it can be added to MahApps directly)

        public static readonly DependencyProperty MaxLengthProperty = DependencyProperty.RegisterAttached("MaxLength", typeof(int), typeof(ComboBoxHelper), new FrameworkPropertyMetadata(0, MaxLengthPropertyChangedCallback), new ValidateValueCallback(MaxLengthValidateValue));

        private static bool MaxLengthValidateValue(object value)
        {
            return ((int)value) >= 0;
        }

        private static void MaxLengthPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var comboBox = dependencyObject as ComboBox;
            if (comboBox != null)
            {
                comboBox.Loaded -= comboBox_Loaded;
                if (e.NewValue != e.OldValue && e.NewValue is int)
                {
                    if (comboBox.IsLoaded)
                    {
                        SetMaxLengthToTextBox(comboBox, (int)e.NewValue);
                    }
                    else
                    {
                        comboBox.Loaded += comboBox_Loaded;
                    }
                }
            }
        }

        static void comboBox_Loaded(object sender, RoutedEventArgs e)
        {
            SetMaxLengthToTextBox((ComboBox)sender, GetMaxLength((ComboBox)sender));
        }

        private static void SetMaxLengthToTextBox(ComboBox comboBox, int maxLength)
        {
            var textBox = comboBox.FindChild<TextBox>("PART_EditableTextBox");
            if (textBox != null)
            {
                textBox.MaxLength = maxLength;
            }
        }

        public static void SetMaxLength(DependencyObject obj, int value)
        {
            obj.SetValue(MaxLengthProperty, value);
        }

        public static int GetMaxLength(DependencyObject obj)
        {
            return (int)obj.GetValue(MaxLengthProperty);
        }

usage

<TextBox Text="{Binding ElementName=ComboBoxTest, Path=(Controls:ComboBoxHelper.MaxLength), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<ComboBox Width="200"
            x:Name="ComboBoxTest"
            Controls:TextBoxHelper.ClearTextButton="True"
            Margin="0, 10, 0, 0"
            SelectedIndex="0"
            Controls:ComboBoxHelper.MaxLength="3"
            IsEditable="True">
    <ComboBoxItem Content="Item 1" />
    <ComboBoxItem Content="Item 2" />
    <ComboBoxItem Content="Very long Item 3 for MahApps.Metro" />
    <ComboBoxItem Content="Item 4" />
</ComboBox>

@Chrullbo
Copy link
Author

Chrullbo commented Jun 1, 2015

thx, that worked like a charm

@Chrullbo Chrullbo closed this as completed Jun 1, 2015
@thoemmi
Copy link
Collaborator

thoemmi commented Jun 1, 2015

LGTM, though I would add comboBox.Loaded -= comboBox_Loaded in comboBox_Loaded.

@punker76
Copy link
Member

punker76 commented Jun 1, 2015

@thoemmi you're right. for MahApps i can use the attached property directly
with binding, so the loaded stuff is not needed.
Am 01.06.2015 19:29 schrieb "Thomas Freudenberg" notifications@github.com:

LGTM, though I would add comboBox.Loaded -= comboBox_Loaded in
comboBox_Loaded.


Reply to this email directly or view it on GitHub
#1948 (comment)
.

@thoemmi
Copy link
Collaborator

thoemmi commented Jun 1, 2015

+1 on integrating it in MA.M 😄

@punker76
Copy link
Member

punker76 commented Jun 1, 2015

@thoemmi -> #1949

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants