与 DisplayMemberPath 的数据绑定转义菜单项中的下划线

Databinding with DisplayMemberPath escapes underscore in menuitem

好的,我们有一个最近的文件菜单选项。我们使用 MVVM 对 MenuItem 条目进行数据绑定,并提供 DisplayMemberPath。但是 WPF 对字符串进行了转义,因此下划线显示为下划线而不是 accesskey

<MenuItem x:Name="RecentScripts" DisplayMemberPath="Caption" Header="Recent _Files" cal:Message.Attach="OpenRecentScript($orignalsourcecontext)">
    <MenuItem.Icon>
        <Image Source="{StaticResource IconOpen}"/>
    </MenuItem.Icon>
</MenuItem>   

https://github.com/AndersMalmgren/FreePIE/blob/recet_files_shortcut/FreePIE.GUI/Views/Main/Menu/MainMenuView.xaml#L35

我们也有自定义主题,但是禁用 MennuItem 的样式没有帮助 https://github.com/AndersMalmgren/FreePIE/blob/recet_files_shortcut/FreePIE.GUI/Themes/ExpressionDark.xaml#L1921

将 DisplayMemberPath 替换为 ItemTemplate。正如解释的那样 here DisplayMemeberPath 是

a template for a single property, shown in a TextBlock

正如@XAMlMAX 提到的,TextBlock 不支持 AccessText,而 Label 支持。

            <MenuItem x:Name="RecentScripts" Header="Recent _Files" cal:Message.Attach="OpenRecentScript($orignalsourcecontext)">
                <MenuItem.Icon>
                    <Image Source="{StaticResource IconOpen}"/>
                </MenuItem.Icon>
                <MenuItem.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Caption}"/>
                    </DataTemplate>
                </MenuItem.ItemTemplate>
            </MenuItem>