views:

73

answers:

1

Hi I create a contextmenu dynamicly using this code

   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);

This menu is attached to textbox.contextmenu property. However menu looks strange. Every text in menu items is centered. It looks quite ugly and frankly speaking I don't know why this looks that way. Could somebody explain me how I can make it align to left ?

A: 

The context menus leave space on the left for icons by default. I would suggest either using that space for icon placement, or override the ContextMenu's ControlTemplate to get a different look and feel.

This example should help get you started on the ControlTemplate:

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

If you already know about the icon space, and are referring to the way MenuItem Headers are positioned by default, then make sure you wrap the Header text in a StackPanel and position its vertical or horizontal layout accordingly.

McStretch
I already know about the icon space. It seems that I have to wrap the header text in a StacPanel. However I don't know exectly how to do it ? Should I for every Header create new stacnpanel ??
george
I was trying to use horizontalalignment and horizontalcontentalignemt property (and set them to Left) but it didnt' work
george
Can you provide more code please? I'm not able to reproduce this on my box, so it's probably some small configuration change that you need to make. What version of .NET are you using? I had some similar issues at work using .NET 3.5, but it had more to do with the vertical alignment -- the horizontal alignment would default to left.
McStretch
I edited first post.
george
I rebuilt the project targeting .NET 3.5 and 3.0, and I still don't get the issue using your code (everything is left aligned). Is there any other code of note? How is your textbox and its container defined? If you can, give me a complete example and I'll see if I can replicate it.
McStretch
I think that I found a problem. I set a style form my textbox <Setter Property="TextAlignment" Value="Center"/> If I replace center with Left menu works fine. However I wanted to have centered text in textbox.
george