public override void OnApplyTemplate() { base.OnApplyTemplate(); templateDescrColl = new TemplateDescriptionCollection();// List<FrameworkElement> parents= Helper.GetAllParent(this);// FindDataTemplatesResources(this.Resources);// foreach (FrameworkElement parent in parents) // { FindDataTemplatesResources(parent.Resources); } FindDataTemplatesResources(System.Windows.Application.Current.Resources);// }
private void FindDataTemplatesResources(ResourceDictionary Resource) { foreach (object xxx in Resource.Values) { DataTemplate dataTemplate = xxx as DataTemplate; if (dataTemplate != null)// a DataTemplate { bool hierarhical=false; if (!templateDescrColl.ExistType(dataTemplate.DataType as Type, out hierarhical))// { TemplateBindingDescr templateBindingDescr = new TemplateBindingDescr();// HierarchicalDataTemplate hdt = xxx as HierarchicalDataTemplate;// HierarchicalDataTemplate here if (hdt != null) { Binding ItemsSourceBinding = hdt.ItemsSource as Binding; string ItemsSourcePath = ItemsSourceBinding.Path.Path; templateBindingDescr.itemSourcePath = ItemsSourcePath;// templateBindingDescr.IsHierathical = true; } Type tType = dataTemplate.DataType as Type; templateBindingDescr.TargetType = tType; // type if (! String.IsNullOrWhiteSpace (FindPatches) && FindPatches.Contains (tType.Name + ":")) // { Match match = Regex.Match (FindPatches, tType.Name + ":([^:]*)");. if (match.Success) { string re = match.Groups [1]. Value; string [] pathes = re.Split (','); templateBindingDescr.BindingPathes.AddRange (pathes); } } else // { FrameworkElement frameworkElement = dataTemplate.LoadContent () as FrameworkElement; // List <FrameworkElement> DependencyObjects = Helper.GetAllVisualChildren (frameworkElement);// if (DependencyObjects!= null) { foreach (FrameworkElement dependencyObject in DependencyObjects) { // BindingExpression BE = System.Windows.Data.BindingOperations.GetBindingExpression (dependencyObject, TextBlock.TextProperty); if (BE!= null) { string path = BE.ParentBinding.Path.Path; templateBindingDescr.BindingPathes.Add (path); } } } } templateDescrColl.Add (templateBindingDescr); // } } } }
/// <summary> /// FindText /// /// </summary> /// <param name="control"></param> /// <param name="FindText"></param> /// <returns></returns> public List<NamedObject> FindObjectByPropertyNames(IEnumerable itemsSource, List<int> path) { List<NamedObject> ret = new List<NamedObject>();// int i = 0; path.Add(i);// foreach (object Item in itemsSource) // { path[path.Count - 1] = i;// TemplateBindingDescr desctiption= templateDescrColl.Get(Item.GetType());// string subItemsPath=desctiption.itemSourcePath;// string ObjectText=String.Empty;// bool ok=false; foreach (string findPath in desctiption.BindingPathes)// { object ItemVal = Item.GetObjectSubItem(findPath); // if (ItemVal != null )// { string ItemText = ItemVal.ToString().ToUpper();// ObjectText += ItemText + " "; if (ItemText.Contains(FindText.ToUpper())) { ok = true; // } } } if (ok)// { List<int> path2 = new List<int>(); path2.AddRange(path); ret.Add(new NamedObject() { Name = ObjectText, Item = Item, Path = path2 });// } if (subItemsPath != null)// { IEnumerable subItems=Item.GetObjectSubItem(subItemsPath) as IEnumerable; if (subItems != null) { List<int> path1 = new List<int>(); path1.AddRange(path); ret.AddRange(FindObjectByPropertyNames(subItems, path1)); } } i++; } return ret; }
int failCount; // // void SelectNodeByPath(ItemsControl control, IEnumerable<int> path) { List<int>L= path.ToList<int>(); if (L.Count == 0) { return; } if (control.Items.Count > L[0]) { do // { control.UpdateLayout(); } while (control.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated); TreeViewItem dObject = control.ItemContainerGenerator.ContainerFromIndex(L[0]) as TreeViewItem; if (dObject == null)// { dObject = GetTreeViewItem(control, L [0]); } if (dObject == null) { MessageBox.Show(" ); } else // { L.RemoveAt(0); if (L.Count == 0) // { dObject.IsSelected = true; dObject.BringIntoView(); } else // { dObject.IsExpanded = true; SelectNodeByPath(dObject, L); // } } } else { MessageBox.Show(" "+control.Items.Count+" > "+L[0]); } }
Source: https://habr.com/ru/post/135274/
All Articles