LoginSignup
1
1

More than 3 years have passed since last update.

MenuItemのRoleはどう決まっているのか?

Posted at

WPFのMenuItemのRoleプロパティについて、どういったMenuItemがどのRoleになるのかの解説が(ざっと見た限り)なかったので、画像入りでメモ。

サンプル

MainWindow.xaml
<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" Title="MainWindow" Height="100" Width="200">
    <Window.Resources>
        <Style TargetType="MenuItem">
            <Setter Property="Header" Value="{Binding Role, RelativeSource={RelativeSource Self}}"/>
        </Style>
    </Window.Resources>
    <Menu  VerticalAlignment="Top">
        <!--TopLevelHeader-->
        <MenuItem>
            <!--SubmenuHeader-->
            <MenuItem>
                <!--SubmenuItem-->
                <MenuItem/>
            </MenuItem>
            <!--SubmenuItem-->
            <MenuItem/>
        </MenuItem>
        <!--TopLevelItem-->
        <MenuItem/>
    </Menu>
</Window>

上記のようなXAMLを記述した場合の表示結果は以下の通り。
無題.png

まとめると

  • Menuの子要素であればTopLevel〇〇、そうでなければSubmenu〇〇
  • 自身が子要素を持てば〇〇Header, 持たなければ〇〇Item

備考

通常だとTopLevelHeaderになる要素でも、IsCheckable = trueならばTopLevelItemになります。参考:MenuItem.cs

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1