怎样动态生成一个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);