Closed ugrgnc closed 5 years ago
@ugrgnc That's not enough information! What are you try to do?
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
@ugrgnc When do you try to find the inner TextBox? And why?
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.
@ugrgnc I think I can't help you enough without a sample app including your behavior.
@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.
PART_EditableTextBox template cannot be found. VisualTreeHelper=>GetChildrenCount() returns 0!, if the combobox component is in the ChildWindow.