How can I incorporate a data bound list of MenuItems to another MenuItem in WPF?

Posted by Julien Poulin on Stack Overflow See other posts from Stack Overflow or by Julien Poulin
Published on 2009-09-09T11:36:27Z Indexed on 2010/06/14 13:22 UTC
Read the original article Hit count: 191

Filed under:
|

I have a 'File' MenuItem were I would like to display a list of recently opened files.

Here is the xaml I have now:

<MenuItem Header="File}">
  <MenuItem Header="Preferences..." Command="{Binding ShowOptionsViewCommand}" />
  <Separator />
  <ItemsControl ItemsSource="{Binding RecentFiles}">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <MenuItem Header="{Binding DisplayPath}" CommandParameter="{Binding}"
            Command="{Binding Path=DataContext.OpenRecentFileCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
        </MenuItem>
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
  <Separator />
  <MenuItem Header="Exit" Command="{Binding CloseCommand}" />
</MenuItem>

However, when I use this code, there is a weird offset around the MenuItems and it looks like there is a container around them. How can I get rid of that?

Here is a screenshot of what it looks like:

alt text

© Stack Overflow or respective owner

Related posts about wpf

Related posts about mvvm