protected override void Attach() { _control = ControlObject as Menu; List <MenuItem> items = new List <MenuItem>(); HeaderedItemsControlUtility.GetChildren(_control, items); foreach (var element in items) { var item = element; string text = HeaderedItemsControlUtility.GetItemText(item); if (string.IsNullOrEmpty(text)) { continue; } RoutedEventHandler opened = (s, e) => { SubmenuOpened(item, new string[] { text }); }; item.SubmenuOpened += opened; RoutedEventHandler click = (s, e) => { Click(item, new string[] { text }); }; item.Click += click; _detach.Add(() => { item.SubmenuOpened -= opened; item.Click -= click; }); } }
void Expanded(TreeViewItem parentItem, string[] texts) { AddSentence(new TokenName(), ".GetItem(" + MakeGetArgs(texts) + ").EmulateChangeExpanded(true", new TokenAsync(CommaType.Non), ");"); RoutedEventHandler closedForGenerate = null; closedForGenerate = (s, ee) => { Collapsed(parentItem, texts); parentItem.Collapsed -= closedForGenerate; }; parentItem.Collapsed += closedForGenerate; parentItem.Dispatcher.BeginInvoke((MyAction) delegate { List <TreeViewItem> items = new List <TreeViewItem>(); HeaderedItemsControlUtility.GetChildren(parentItem, items); foreach (var element in items) { var item = element; string text = HeaderedItemsControlUtility.GetItemText(item); if (string.IsNullOrEmpty(text)) { continue; } List <string> nextTexts = new List <string>(texts); nextTexts.Add(text); RoutedEventHandler opened = (s, e) => { Expanded(item, nextTexts.ToArray()); }; item.Expanded += opened; RoutedEventHandler click = (s, e) => { SelectedChanged(item, nextTexts.ToArray()); }; item.Selected += click; item.Unselected += click; RoutedEventHandler closed = null; closed = (s, ee) => { item.Expanded -= opened; item.Selected -= click; item.Unselected -= click; parentItem.Collapsed -= closed; }; parentItem.Collapsed += closed; } }); }
void AttachChildren(int[] indices, string[] texts, ItemsControl itemsControl) { //仮想化対応 50ミリ周期でイベントハンドリングしていないTreeViewItemを監視する //初回はすぐに実行されるようにする var timer = new System.Windows.Forms.Timer { Interval = 1 }; int[] next = null; System.Windows.Forms.MethodInvoker eventConnection = () => { timer.Interval = 50; //子要素取得 var exists = GetTreeChildren(itemsControl, next, out var notExists); foreach (var item in exists) { string text = HeaderedItemsControlUtility.GetItemText(item.Value); List <int> nextIndices = new List <int>(indices); List <string> nextTexts = new List <string>(texts); nextIndices.Add(item.Key); nextTexts.Add(text); EventConnection(item.Value, false, IsTextKey, nextIndices.ToArray(), nextTexts.ToArray()); } //仮想化でまだ存在していない要素があればそれを監視させる next = notExists; //全てアタッチしたら終了 if (next.Length == 0) { timer?.Stop(); } }; //タイマ処理 timer.Tick += (_, __) => { try { eventConnection(); } catch { timer.Stop(); } }; timer.Start(); }
void SubmenuOpened(MenuItem parentItem, string[] texts) { parentItem.Dispatcher.BeginInvoke((MyAction) delegate { List <MenuItem> items = new List <MenuItem>(); HeaderedItemsControlUtility.GetChildren(parentItem, items); foreach (var element in items) { var item = element; string text = HeaderedItemsControlUtility.GetItemText(item); if (string.IsNullOrEmpty(text)) { continue; } List <string> nextTexts = new List <string>(texts); nextTexts.Add(text); RoutedEventHandler opened = (s, e) => { SubmenuOpened(item, nextTexts.ToArray()); }; item.SubmenuOpened += opened; RoutedEventHandler click = (s, e) => { Click(item, nextTexts.ToArray()); }; item.Click += click; RoutedEventHandler closed = null; closed = (s, ee) => { item.SubmenuOpened -= opened; item.Click -= click; parentItem.SubmenuClosed -= closed; }; parentItem.SubmenuClosed += closed; } }); }
void AttachChildren(Visual parent, List <MenuItem> attachedChildren, string[] texts) { List <MenuItem> items = new List <MenuItem>(); HeaderedItemsControlUtility.GetChildren(parent, items); foreach (var element in items) { var item = element; string text = HeaderedItemsControlUtility.GetItemText(item); if (string.IsNullOrEmpty(text)) { continue; } List <string> nextTexts = new List <string>(texts); nextTexts.Add(text); //まだイベントにアタッチしてない場合 if (!attachedChildren.Contains(item)) { attachedChildren.Add(item); RoutedEventHandler click = (s, e) => { Click(item, nextTexts.ToArray()); }; item.Click += click; _detach.Add(() => { item.Click -= click; }); } //子アイテムにアタッチ AttachChildren(item, attachedChildren, nextTexts.ToArray()); } }
static TreeViewItem GetItemEx(TreeView tree, ItemsControl parent, string[] headerTexts, int index) { for (int i = 0; i < parent.Items.Count; i++) { var item = GetItemAndScrollIntoView(tree, parent, i); var text = HeaderedItemsControlUtility.GetItemText(item); if (text == headerTexts[index]) { if (index == headerTexts.Length - 1) { item.BringIntoView(); return(item); } else { ShowNextItem(item); return(GetItemEx(tree, item, headerTexts, index + 1)); } } } throw new NotSupportedException(ResourcesLocal3.Instance.ErrorNotFoundItem); }
public override bool ConvertChildClientPoint(ref System.Drawing.Point clientPointWinForms, out string childUIObject) { childUIObject = string.Empty; var clientPoint = new Point(clientPointWinForms.X, clientPointWinForms.Y); //指定座標の要素取得 var hitElement = PointUtility.GetPosElement(clientPoint, _control); if (hitElement == null) { return(false); } //TreeItemをたどる var items = new List <TreeViewItem>(); foreach (var x in TreeUtilityInTarget.VisualTree(hitElement, TreeRunDirection.Ancestors)) { if (Equals(x, _control)) { break; } var item = x as TreeViewItem; if (item != null) { items.Add(item); } } if (items.Count == 0) { return(false); } //座標変換 var screenPos = _control.PointToScreen(clientPoint); var childPoint = items[0].PointFromScreen(screenPos); clientPointWinForms.X = (int)childPoint.X; clientPointWinForms.Y = (int)childPoint.Y; //子取得用文字列作成 items.Reverse(); var texts = new List <string>(); foreach (var x in items) { texts.Add(HeaderedItemsControlUtility.GetItemText(x)); } var indices = new List <int>(); ItemsControl itemsControl = _control; foreach (var x in items) { indices.Add(itemsControl.ItemContainerGenerator.IndexFromContainer(x)); itemsControl = x; } childUIObject = $".GetItem({MakeGetArgs(IsTextKey, indices.ToArray(), texts.ToArray())})"; return(true); }