WPF奇怪的上下文菜单行为

时间:2010-09-17 22:14:48

标签: wpf contextmenu

我使用此代码动态创建上下文菜单

   protected MenuItem itemAdd, itemDelete, itemSelectBranch, itemDeleteClasp;
    protected MenuItem itemCut, itemCopy, itemPaste, itemAddParent, itemPasteWithChildren;
    protected MenuItem itemAddTask, itemAddExtTask, itemAddMileStone;
 menu = new ContextMenu();
        itemAdd = new MenuItem
                      {
                          HorizontalAlignment = HorizontalAlignment.Left,
                          HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch,
                          Header = "Add"
                      };
        itemDelete = new MenuItem
                         {
                             HorizontalAlignment = HorizontalAlignment.Left,
                             HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch,
                             Header = "Delete"
                         };
        itemSelectBranch = new MenuItem
        {
            HorizontalAlignment = HorizontalAlignment.Left,
            HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch,
                                   Header = "Select"
                               };
        itemAddTask = new MenuItem
        {
            HorizontalAlignment = HorizontalAlignment.Left,
            HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch,

                              Header = "Task",

                          };
        itemAddParent = new MenuItem
                            {
                                HorizontalAlignment = HorizontalAlignment.Left,
                                HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch,
                                Header = "Parent"
                            };

        itemAddMileStone = new MenuItem
                               {
                                   HorizontalAlignment = HorizontalAlignment.Left,
                                   HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch,
                                   Header = "Do sth",

                               };
        itemAddExtTask = new MenuItem
                             {
                                 HorizontalAlignment = HorizontalAlignment.Left,
                                 HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch,
                                 Header = "Do sth"
                             };
        itemDelete.HorizontalAlignment = HorizontalAlignment.Left;
        itemDelete.Click += itemDelete_Click;



        itemDeleteClasp = new MenuItem
                              {
                                  HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch,
                                  HorizontalAlignment = HorizontalAlignment.Left,
                                  Header = "Do sthm"
                              };
        itemDeleteClasp.Click += itemDeleteClasp_Click;
        itemCopy = new MenuItem {Header = Do sth",HorizontalAlignment = System.Windows.HorizontalAlignment.Left};

        itemCopy.Click += itemCopy_Click;
        itemCut = new MenuItem
                      {
                          HorizontalAlignment = HorizontalAlignment.Left,
                          Header = "Cut"
                      };

        itemPaste = new MenuItem
                        {
                            HorizontalAlignment = HorizontalAlignment.Left,
                            Header = "Paste"
                        };

        itemAddParent = new MenuItem
                            {
                                HorizontalAlignment = HorizontalAlignment.Left,
                                Header = "Do sth"
                            };

        itemPasteWithChildren = new MenuItem
                                    {
                                        HorizontalAlignment = HorizontalAlignment.Left,
                                        Header = "some long text"
                                    };
        itemPasteWithChildren.Click += itemPasteWithChildren_Click;
        itemAddParent.Click += itemAddParent_Click;
        itemPaste.Click += itemPaste_Click;
        itemCut.Click += itemCut_Click;
        itemAddTask.Click += itemAddTask_Click;
        itemAddExtTask.Click += itemAddExtTask_Click;
        itemAddMileStone.Click += itemAddMileStone_Click;
        itemAddParent.Click += itemAddParent_Click;
        itemSelectBranch.Click += itemSelectBranch_Click;
        itemAdd.Items.Add(itemAddTask);
        itemAdd.Items.Add(itemAddExtTask);
        itemAdd.Items.Add(itemAddMileStone);
        itemAdd.Items.Add(itemAddParent);
        menu.Items.Add(itemAdd);
        menu.Items.Add(itemDelete);
        menu.Items.Add(itemDeleteClasp);
        menu.Items.Add(itemSelectBranch);
        menu.Items.Add(itemCopy);
        menu.Items.Add(itemCut);
        menu.Items.Add(itemPaste);

此菜单附加到textbox.contextmenu属性。但菜单看起来很奇怪。菜单项中的每个文本都居中。它看起来很丑陋,坦率地说,我不知道为什么会这样。有人可以解释我如何让它与左边对齐吗?

1 个答案:

答案 0 :(得分:0)

默认情况下,上下文菜单会在左侧留出空间用于图标。我建议使用该空间进行图标放置,或者覆盖ContextMenu的ControlTemplate以获得不同的外观。

此示例可帮助您开始使用ControlTemplate

http://msdn.microsoft.com/en-us/library/ms744758(VS.85).aspx

如果您已经了解图标空间,并且指的是默认情况下MenuItem标题的定位方式,那么请确保将Header文本包装在StackPanel和位置相应的垂直或水平布局。