怎样动态生成一个button后再添加一个ContextMenu控件
怎样动态生成一个button后再在他的属性上添加一个ContextMenu控件。
[最优解释]
Button btn = new Button();
假设Button的父容器对象是grid,
那么grid.Children.Add(btn);
然后设置菜单
ContextMenu menu = new ContextMenu();
MenuItem menuItem1 = new MenuItem();
menuItem1.Header = "This is Menu Item 1";
menuItem1.Click += new RoutedEventHandler(MenuItem_Click);
menu.Items.Add(menuItem1);
MenuItem menuItem2 = new MenuItem();
menuItem2.Header = "This is Menu Item 2";
menuItem2.Click += new RoutedEventHandler(MenuItem_Click);
menu.Items.Add(menuItem2);
ContextMenuService.SetContextMenu(btn , menu);
[其他解释]
之前解决了,还是谢谢了