private void ItemButton_Click(object sender, RoutedEventArgs e) { ContextMenu cm = GridItem?.FindResource("ContextMenuItem") as ContextMenu; if (cm != null) { cm.PlacementTarget = sender as Button; cm.IsOpen = true; } }
private void ItemButton_Click(object sender, RoutedEventArgs e) { // access ContextMenu cm = GridItem?.FindResource("ContextMenuItem") as ContextMenu; var data = this.DataContext as DocumentEntity; if (cm == null || data == null) { return; } // clear old items (very stupid) while (cm.Items.Count > 4) { cm.Items.RemoveAt(4); } // add new items if (data.Relations != null && data.Relations.Count > 0) { cm.Items.Add(new Separator()); foreach (var reltup in data.Relations) { var drt = reltup.Item1; var re = reltup.Item2; if (re == null || re.Count < 1) { continue; } var mi = new MenuItem(); mi.Header = "" + drt.ToString() + ": " + re.Last.value; mi.Icon = " \x2794"; mi.Click += MenuItem_Click; mi.Tag = reltup; cm.Items.Add(mi); } } // show cm.PlacementTarget = sender as Button; cm.IsOpen = true; }