-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Comments
@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> |
thx, that worked like a charm |
LGTM, though I would add |
@thoemmi you're right. for MahApps i can use the attached property directly
|
+1 on integrating it in MA.M 😄 |
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
The text was updated successfully, but these errors were encountered: