0

The following code creates MenuItems from a list inside another MenuItem. I'm recreating a recent files option but, it is creating a container inside another container when generating the sub-item.

<MenuItem Header="_Recent" Height="25" ItemsSource="{x:Static cl_resources:MenuActions.Recent}">
   <MenuItem.Icon>
      <Image Source="/Resources/Icons/MenuBar/list.ico" Height="25"/>
   </MenuItem.Icon>
   <MenuItem.ItemTemplate>
     <DataTemplate>
        <MenuItem Header="{Binding}" Click="MenuItem_Recent_Click" Height="25" Cursor="Hand">
           <MenuItem.Icon>
              <Image Source="/Resources/Icons/MenuBar/document.ico" Height="25"/>
           </MenuItem.Icon>
        </MenuItem>
      </DataTemplate>
   </MenuItem.ItemTemplate>
</MenuItem>

This is the Result that I get, I want to only display what's labeled as 1 and remove the part 2.

I hope the question was clear enough, thank you.

Community
  • 1
  • 1
Ricardo
  • 1
  • 1

1 Answers1

0

Replace the MenuItem in the DataTemplate with StackPanel, Grid or whatever Panel you like.

There was a similar question asked already. Basically MenuItem.ItemTemplate shouldn't contain MenuItem. The parent MenuItem creates its sub-MenuItems automatically and sets their content to the specified MenuItem.ItemTemplate.

<DataTemplate>
    <StackPanel Orientation="Horizontal" Click="MenuItem_Recent_Click" Height="25" Cursor="Hand">
        <Image Source="/Resources/Icons/MenuBar/document.ico" Height="25"/>
        <TextBlock Text="{Binding}"/>
    </StackPanel>
</DataTemplate>
Adam Štafa
  • 353
  • 1
  • 13
  • I'd seen that answer and have tried before but it gives me an error when parsing the XAML file, in the last line of the stackpanel. I'm thinking that I'll have to fidget with the item source binding. – Ricardo Oct 11 '19 at 08:35