通过数据源找到控件 再通过控件找到模板数据
作者:互联网
private void GetCheckBoxVal() { if (treeView != null) { ContentPresenter myContentPresenter = null; DataTemplate myDataTemplate = null; CheckBox ck = null; TextBlock tb = null; foreach (var item in treeView.Items) { DependencyObject obj = treeView.ItemContainerGenerator.ContainerFromItem(item); ; myContentPresenter = FindVisualChild<ContentPresenter>(obj); myDataTemplate = myContentPresenter.ContentTemplate; ck = (CheckBox)myDataTemplate.FindName("ck", myContentPresenter) as CheckBox; tb = (TextBlock)myDataTemplate.FindName("txt", myContentPresenter) as TextBlock; } } } private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null && child is childItem) return (childItem)child; else { childItem childOfChild = FindVisualChild<childItem>(child); if (childOfChild != null) return childOfChild; } } return null; }
标签:控件,obj,找到,数据源,myContentPresenter,child,myDataTemplate,null,childItem 来源: https://www.cnblogs.com/yuanchao/p/11597020.html