处理WPF菜单HeaderStringFormat和访问键等 [英] Dealing with WPF Menu HeaderStringFormat and Access Keys among other things

查看:169
本文介绍了处理WPF菜单HeaderStringFormat和访问键等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.因此,我希望我的应用程序像Visual Studio一样,在其主菜单中显示保存"和另存为..."项目.即将{当前文件}另存为..."

Okay. So I want my application to display in its main menu the "Save" and "Save As..." items just like Visual Studio does; i.e. "Save {current file}" and "Save {current file} As..."

我还想拥有普通的访问键(分别为"S"和"A").

I would also like to have the normal access keys ("S" and "A", respectively).

我已经提出了两种解决方案,但都不是很理想.

I've come up with two solutions, but neither is very desirable.

  • 我可以不用专门在xaml中创建主菜单,而可以 在MainWindowViewModel中全部创建它,这样我就可以完全控制生成的MenuItems中的内容.但是,我认为这将违反MVVM(这次我将尝试严格遵守),因为我必须在ViewModel中包括对每个MenuItem的Icon的引用.另外,似乎有点混乱.

  • Instead of creating the main menu exclusively in xaml, I could create it all in the MainWindowViewModel so I'd have full control over what goes into the generated MenuItems. However, I feel that this would be a violation of MVVM (which I'm attempting to abide by very strictly this time around) as I would have to include references to each MenuItem's Icon in the ViewModel. Plus it seems a little messy.

我可以像这样规定这两个特定MenuItems(也许将来的)的标题,但是最后我得到的MenuItem不仅在标题中带有下划线,而且不包含访问密钥.

I can stipulate the header of just these two specific MenuItems (and perhaps future ones) like so, but then I end up getting a MenuItem that not only has a underscore in the header, but also does not contain an access key.

<MenuItem Header="{Binding CurrentFileName}"
          HeaderStringFormat="Save {0} _As...">

我该怎么办?

推荐答案

Whelp,弄清楚了.至少关于如何使用XAML中描述的整个主菜单来完成它.只是将标头内容设置为AccessText控件而不是字符串,它的工作原理就像一个超级按钮.

Whelp, figured it out. At least about how to get it done with the whole main menu described in XAML. Just made the header content an AccessText control instead of a string and it works like a charm.

<MenuItem>
    <MenuItem.Style>
        <Style TargetType="{x:Type MenuItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasSelection}" Value="false">
                    <Setter Property="IsEnabled" Value="false"/>
                    <Setter Property="Header">
                        <Setter.Value>
                            <AccessText Text="Save Selected File _As..."/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding HasSelection}" Value="true">
                    <Setter Property="IsEnabled" Value="true"/>
                    <Setter Property="Header">
                        <Setter.Value>
                            <AccessText Text="{Binding SelectedFile.Filename, StringFormat=Save {0} _As...}"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </MenuItem.Style>
</MenuItem>

这篇关于处理WPF菜单HeaderStringFormat和访问键等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆