namg_engr
namg_engr

Reputation: 359

WPF split up menu structure into separate user controls

My menu structure is quite large and it is currently in the mainwindow. I'd like to separate each main menu into separate user controls. For Example, I'd like to put "File" into its own user control, "Edit" into its own user control, etc. Their are a few issues: There appears margin (or padding?) surrounding each main menu item. Also, hovering over an adjacent main menu item does not open it. How can these issues be resolved? Or is there another way to separate the main menu items?

MainWindow.XAML

<Window x:Class="WpfApp1.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"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <Menu>
        <local:ucFile/>
        <local:ucEdit/>
    </Menu>
</Grid>

ucFile.XAML

<UserControl x:Class="WpfApp1.ucFile"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d">
<Menu>
    <MenuItem Header="_File" Name="menuFile">
        <MenuItem Header="_Save" />
        <Separator />
        <MenuItem Header="_Close"/>
    </MenuItem>
</Menu>

ucEdit.XAML

<UserControl x:Class="WpfApp1.ucEdit"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d">
<Menu>
    <MenuItem Header="_Edit" Name="menuEdit">
        <MenuItem Header="_Cut" />
        <MenuItem Header="_Paste"/>
    </MenuItem>
</Menu>

Upvotes: 1

Views: 425

Answers (1)

Kinnara
Kinnara

Reputation: 76

Menu should contain MenuItems, not UserControls. The following should work:

Menu:

<Menu>
    <local:FileMenuItem />
</Menu>

MenuItem XAML:

<MenuItem
    x:Class="WpfApp16.FileMenuItem"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    mc:Ignorable="d"
    Header="_File"
    Name="menuFile">
    <MenuItem Header="_Save" />
    <Separator />
    <MenuItem Header="_Close" />
</MenuItem>

MenuItem C#:

namespace WpfApp16
{
    public partial class FileMenuItem
    {
        public FileMenuItem()
        {
            InitializeComponent();
        }
    }
}

Upvotes: 2

Related Questions