美文网首页WPF
ContextMenuOpening

ContextMenuOpening

作者: 落地成佛 | 来源:发表于2019-10-11 08:57 被阅读0次
     <Rectangle Fill="Purple" Width="200" Height="100" ContextMenuOpening="AddItemToCm">
          <Rectangle.ContextMenu>
            <ContextMenu>
              <MenuItem Header="Item1"/>
              <MenuItem Header="Item2"/>
              <MenuItem Header="Item3"/>
            </ContextMenu>
          </Rectangle.ContextMenu>
        </Rectangle>
    
      private void AddItemToCm(object sender, ContextMenuEventArgs e)
            {
                //check if Item4 is already there, this will probably run more than once
                var fe = e.Source as FrameworkElement;
                var cm = fe.ContextMenu;
                if (cm.Items.Cast<MenuItem>().Any(mi => (string) mi.Header == "Item4"))
                {
                    return;
                }
                var mi4 = new MenuItem {Header = "Item4"};
                fe.ContextMenu.Items.Add(mi4);
            }
    

    相关文章

      网友评论

        本文标题:ContextMenuOpening

        本文链接:https://www.haomeiwen.com/subject/pdyqyctx.html