查找作为通过代码生成的默认控件模板 ComboBox 一部分的 TextBlock
我试图找到组合框控件模板内的 TextBlock。 仅当在 XAML 中声明了组合框时,使用 VisualTreeHelpar.GetChildrenCount 才有效。在这种情况下,GetChildrenCount 返回 1 并且可以进行递归搜索。
但是,如果我使用代码将组合声明为 Window 类的成员,分配并将其设置到其位置,则函数 GetChildrenCount 返回 0。当我在这种情况下运行 snoop 时,它会显示组合子级层次结构。我希望能够像 snoop 一样搜索组合框。
任何帮助将不胜感激。
代码:
ComboBox mCombo = null;
private void Windows_Loaded(object sender, RoutedEventArgs e)
{
mCombo = new ComboBox;
mGrid.Children.Add(mCombo);
Grid.SetRow(mCombo,0);
int count = VisualTreeHelpar.GetChildrenCount(mCombo);
}
I'm trying to find the TextBlock that is inside the control template of a comboBox.
using VisualTreeHelpar.GetChildrenCount is working only if the comboBox is declared in XAML.In that case GetChildrenCount returns 1 and a recursive search is possible.
However, if I declare the combo as a member of the Window class using code, allocated and setting it to its place, the function GetChildrenCount return 0. When I run snoop in this scenario It shows the combo children hierarchy. I want to be able to search the comboBox just as snoop does.
Any help would be appreciated.
code:
ComboBox mCombo = null;
private void Windows_Loaded(object sender, RoutedEventArgs e)
{
mCombo = new ComboBox;
mGrid.Children.Add(mCombo);
Grid.SetRow(mCombo,0);
int count = VisualTreeHelpar.GetChildrenCount(mCombo);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用ComboBox的ApplyTemplate方法。然后,您应该能够找到您需要的东西。
Call the ApplyTemplate method of ComboBox. Then, you should be able to find what you need.