代码片段如下:
<StackPanel>
<ListBox Height="440" Width="370" x:Name="wtListBox" Background="White">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Name="pText" Width="70"></TextBlock> // 1
<TextBlock Name="xText" Width="100"></TextBlock> // 2
<TextBlock Name="yText" Width="100"></TextBlock> // 3
<TextBlock Name="zText" Width="100"></TextBlock> // 4
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
我想通过页面间值传递给上面4个TextBlock的Text属性赋值,但在OnNavigatedTo()方法中点不出pText,赋值无法进行,用什么方法搞出来。求教。
datetemplate里面没办法直接搞出来,你只能借助类似visualtreehelpex等视觉树/逻辑树的相关查找子元素的方法来定位到你的textBlock了
当然还有中简单的方法,就是通过将listitem跟模型双向绑定的方法来解决这个问题,这样你只要通过模型中特定的属性就可以实现对textblock值得操作了
是这样啊,那能不能用视觉树/逻辑树的方法举个例子,这样的方法没用过,谢谢
@AkazaAkari:
这里又有以前写的一个视觉树(父/子)对象查找的辅助类
public static class VisualTreeHelperEx { public static DependencyObject FindAncestorByType(DependencyObject element, Type type, bool specificTypeOnly) { if (element == null) { return null; } if (specificTypeOnly ? (element.GetType() == type) : (element.GetType() == type || element.GetType().IsSubclassOf(type))) { return element; } return VisualTreeHelperEx.FindAncestorByType(VisualTreeHelper.GetParent(element), type, specificTypeOnly); } public static T FindAncestorByType<T>(DependencyObject depObj) where T : DependencyObject { if (depObj == null) { return default(T); } if (depObj is T) { return (T)((object)depObj); } T t = default(T); return VisualTreeHelperEx.FindAncestorByType<T>(VisualTreeHelper.GetParent(depObj)); } public static Visual FindDescendantByName(Visual element, string name) { if (element == null) { return null; } if (element is FrameworkElement && (element as FrameworkElement).Name == name) { return element; } Visual visual = null; if (element is FrameworkElement) { (element as FrameworkElement).ApplyTemplate(); } for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { Visual element2 = VisualTreeHelper.GetChild(element, i) as Visual; visual = VisualTreeHelperEx.FindDescendantByName(element2, name); if (visual != null) { break; } } return visual; } public static Visual FindDescendantByType(Visual element, Type type) { return VisualTreeHelperEx.FindDescendantByType(element, type, true); } public static Visual FindDescendantByType(Visual element, Type type, bool specificTypeOnly) { if (element == null) { return null; } if (specificTypeOnly ? (element.GetType() == type) : (element.GetType() == type || element.GetType().IsSubclassOf(type))) { return element; } Visual visual = null; if (element is FrameworkElement) { (element as FrameworkElement).ApplyTemplate(); } for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { Visual element2 = VisualTreeHelper.GetChild(element, i) as Visual; visual = VisualTreeHelperEx.FindDescendantByType(element2, type, specificTypeOnly); if (visual != null) { break; } } return visual; } public static IEnumerable<Visual> FindAllDescendantByType(Visual element, Type type, bool specificTypeOnly) { if (element == null) { return null; } if (specificTypeOnly ? (element.GetType() == type) : (element.GetType() == type || element.GetType().IsSubclassOf(type))) { return new List<Visual> { element }; } List<Visual> list = new List<Visual>(); if (element is FrameworkElement) { (element as FrameworkElement).ApplyTemplate(); } for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { Visual element2 = VisualTreeHelper.GetChild(element, i) as Visual; list.AddRange(VisualTreeHelperEx.FindAllDescendantByType(element2, type, specificTypeOnly)); } return list; } public static IEnumerable<T> FindAllDescendantByType<T>(Visual element) where T : Visual { IEnumerable<Visual> source = VisualTreeHelperEx.FindAllDescendantByType(element, typeof(T), true); return source.Cast<T>(); } public static T FindDescendantByType<T>(Visual element) where T : Visual { Visual visual = VisualTreeHelperEx.FindDescendantByType(element, typeof(T)); return (T)((object)visual); } public static Visual FindDescendantWithPropertyValue(Visual element, DependencyProperty dp, object value) { if (element == null) { return null; } if (element.GetValue(dp).Equals(value)) { return element; } Visual visual = null; if (element is FrameworkElement) { (element as FrameworkElement).ApplyTemplate(); } for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { Visual element2 = VisualTreeHelper.GetChild(element, i) as Visual; visual = VisualTreeHelperEx.FindDescendantWithPropertyValue(element2, dp, value); if (visual != null) { break; } } return visual; } }
@visonme: 谢谢,我研究研究