punker76 / MahApps.Metro.SimpleChildWindow

A simple child window for MahApps.Metro
MIT License
374 stars 55 forks source link

can't be found PART_EditableTextBox Template #69

Closed ugrgnc closed 5 years ago

ugrgnc commented 6 years ago

PART_EditableTextBox template cannot be found. VisualTreeHelper=>GetChildrenCount() returns 0!, if the combobox component is in the ChildWindow.

punker76 commented 6 years ago

@ugrgnc That's not enough information! What are you try to do?

ugrgnc commented 6 years ago

Hello,

I use SimpleChildWindow for my project. in the ChildWindow I have a Combobox. I want to get "PART_EditableTextBox" template of Combobox with VisualTreeHelper class. but It cannot be found! if the Combobox is in the MetroWindow or Window, it can be found.

// textbox cannot be found! var textbox1 = UIHelper.FindChild(comboBox, "PART_EditableTextBox"); var textBox2 = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox;

punker76 commented 6 years ago

@ugrgnc When do you try to find the inner TextBox? And why?

ugrgnc commented 6 years ago

I want to use Combobox in Editable Mode to search, because of that I want to set some property of the Textbox like "MaxLength", "CharacterCasing". I use a Behavior for Combobox. I try to get Template with VisualTreeHelper and FindName function. but I did not succeed. it returns always null.

punker76 commented 6 years ago

@ugrgnc I think I can't help you enough without a sample app including your behavior.

ugrgnc commented 6 years ago

@punker76

Sample application:

SimpleChildWindowTest.zip

punker76 commented 5 years ago

@ugrgnc Thx for the sample! In WPF it could be that a template is still not applied when the loaded event occurs. To ensure this you can simply call ApplyTemplate on the element:

private static void ApplyCharacterCasing(ComboBox comboBox)
{
    // ensure that the template is applied
    comboBox.ApplyTemplate();

    // try with VisualTreeHelper class
    // textbox cannot be found!

    var textbox1 = UIHelper.FindChild<TextBox>(comboBox, "PART_EditableTextBox");
    if (textbox1 != null)
    {
        textbox1.CharacterCasing = GetCharacterCasing(comboBox);
        textbox1.MaxLength = GetMaxLength(comboBox);
    }

    // try with FindName function
    // textbox cannot be found!
    if (comboBox.Template.FindName("PART_EditableTextBox", comboBox) is TextBox textbox2)
    {
        textbox2.CharacterCasing = GetCharacterCasing(comboBox);
        textbox2.MaxLength = GetMaxLength(comboBox);
    }
}

Now both versions should be work for you.