Can I inherit from MenuItem?

23 Views Asked by At

I know this is a little unorthodox but hear me out.

I have menu that I have completely customized (the popup, appearance, size, everything). My question is, can I make my own custom MenuItem and include custom properties?

For example:

public class CustomMenuItem: MenuItem
{
    public bool IsDefault
    {
        get { return (bool)GetValue(IsDefaultProperty); }
        set { SetValue(IsDefaultProperty, value); }
    }

    // Using a DependencyProperty as the backing store for IsDefault.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsDefaultProperty =
        DependencyProperty.Register("IsDefault", typeof(bool), typeof(CustomMenuItem), new PropertyMetadata(false));
}

The idea would be to have a menu like this:

<Menu ItemsSource="{Binding MenuItems}">
    <Menu.ItemContainerStyle>
        <Style TargetType="{x:Type CustomMenuItem}">
            <Setter Property="Header" Value="Some viewmodel property"/>
            <Setter Property="IsDefault" Value="Some viewmodel property"/>
        </Style>
    </Menu.ItemContainerStyle>
    <Menu.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Vertical" Width="340" />
    </ItemsPanelTemplate>
    </Menu.ItemsPanel>
<Menu>

I can get code like this compile but at runtime the menu doesn't appear.

Am I missing something?

0

There are 0 best solutions below