0

I've created a Recent Files menu item to which I bind a collection of recent file view models.

<MenuItem Header="_Recent Files" ItemsSource="{Binding RecentFiles}" Command="{x:Static pm:MainWindow.RecentFileListCommand}">
    <MenuItem.ItemContainerStyle>
        <Style>
            <Setter Property="MenuItem.Header" Value="{Binding}"/>
            <Setter Property="MenuItem.Command" Value="{x:Static pm:MainWindow.RecentFileOpenCommand}"/>
            <Setter Property="MenuItem.CommandParameter" Value="{Binding FilePath}"/>
        </Style>
    </MenuItem.ItemContainerStyle>
</MenuItem>

I want the list numbered, and I want the numbers to be used as the shortcut (exactly how Visual Studio does it). My view models have a Number field and Name field. I override the ToString() method to return a header including the underscore:

return string.Format("_{0} {1}", Number, FilePath);

However, when bound, the underscore is seen as a literal and displays as part of the header text (not the shortcut). I also tried using HeaderStringFormat without luck:How can I get WPF to see the underscore as the shortcut and not a literal string?

Jason Butera
  • 2,376
  • 3
  • 29
  • 46
  • You might try giving the menuitem a ContentTemplate with an `AccessText` control in it, [as in this answer](http://stackoverflow.com/a/7416523/424129) (that's for a button, but MenuItem as a ContentTemplate property too -- inherited from the same ancestor class IIRC). – 15ee8f99-57ff-4f92-890c-b56153 Jun 30 '16 at 19:52

1 Answers1

1

Could try to return an AccessText, though it might break separation of concerns.

As mentioned by Matthew Whited, a cleaner way would be to return the string only and use a converter to turn it into an AccessText. Alternatively use the HeaderTemplate and create it there.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400