0

As shown in below code, The contextmenu has four menuitems one, two, three and four. MenuItem three has subitems embeded in list box and they are test1, test2 and test3. The width of test1 test2 and test3 is less than parent item (i.e menuitem or submenuitem). How to make the width stretched to takethe width of the parent.

<Window.Resources>

    <ContextMenu x:Key="CustomContextMenu">
        <MenuItem Header="One"></MenuItem>
        <MenuItem Header="two"></MenuItem>
        <MenuItem Header="three" Margin="0"  x:Name="menu_three" 
                  Padding="0" 
                  Grid.IsSharedSizeScope="True" >
            <StackPanel  Background="Blue" Margin="0" 
                         Width="{Binding Path=PlacementTarget.Parent.Width, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}}}">
                <ListBox HorizontalAlignment="Stretch">
                    <ListBoxItem Content="test1"></ListBoxItem>
                    <ListBoxItem Content="test2"></ListBoxItem>
                    <ListBoxItem Content="test3"></ListBoxItem>
                </ListBox>
            </StackPanel>
        </MenuItem>
        <MenuItem Header="four"></MenuItem>
    </ContextMenu>

</Window.Resources>

Thanks

1 Answers1

0

Remove ListBox and use MenuItem to show your sub menu items

<ContextMenu>
    <MenuItem Header="One"></MenuItem>
    <MenuItem Header="two"></MenuItem> 
    <MenuItem Header="three" Margin="0"  x:Name="menu_three" Padding="0" >
       <MenuItem Header="test1"></MenuItem>
       <MenuItem Header="test2"></MenuItem>
       <MenuItem Header="test3"></MenuItem>
    </MenuItem>
    <MenuItem Header="four"></MenuItem>
</ContextMenu>
Nitesh
  • 7,261
  • 3
  • 30
  • 25
  • I replace list with menu and used collection as the source of and facing same problem. – user2585638 Jul 16 '13 at 13:13
  • If you are setting ItemsSource then no need to create a DataTemplate because MenuItems will be added to Items collection of your root MenuItem when you set ItemsSource. So remove the ItemTemplate you setting. Also Set DisplayMemberPath="Name" – Nitesh Jul 16 '13 at 13:46
  • That works great. But I have menu with a list of check box and OK, Cancel button. So I use the first approach of MenuItem with a stackpanl which had a checkbox list, Ok and Cancel buttons. How can avoid extra space or is there any other way with out Stackpanel. – user2585638 Jul 16 '13 at 17:09
  • You can do that by using a DataTemplate to format your data. You create a DataTemplate in which you will put your controls that you want to show in every MenuItem(In your case a CheckBox, OK and Cancel button). Then apply this DataTemplate to ItemTemplate property of your MenuItem. – Nitesh Jul 17 '13 at 04:36
  • Initially thank you. The challenge part is number of checkboxes are dynamic through binding and OK,Cancel buttons are static. I suppose for this case single DataTemplate is not sufficient. Multiple DataTemplate or Hierarchical datatemplate is required. I could not find any sample which explains of how to achieve this. I appriciate your help. – user2585638 Jul 17 '13 at 14:06