代码之家  ›  专栏  ›  技术社区  ›  Anders

使用DisplayMemberPath进行数据绑定会在menuitem中转义下划线

  •  1
  • Anders  · 技术社区  · 7 年前

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

    enter image description here

    <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

    1 回复  |  直到 7 年前
        1
  •  3
  •   3615    7 年前

    用ItemTemplate替换DisplayMemberPath。就像解释的那样 here DisplayMemeberPath是

    单个属性的模板,显示在文本块中

    正如@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>