我有一个FlipView控件,其DataTemplate定义如下:
在后面的代码中,我需要访问名为“ xxxTB”的TextBlock。这是我要做的代码:
public IEnumerableFindVisualChildren (DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { yield return (T)child; } foreach (T childOfChild in FindVisualChildren (child)) { yield return childOfChild; } } } } public void TestMethod() { foreach (var item in FindVisualChildren (this)) { if (timeLine.Name == "xxxTB") { } } }
但是,当它在VisualTree中找到FlipView时,它将从:中返回,for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
因为VisualTreeHelper.GetChildrenCount(depObj)
不返回任何内容。
任何的想法?