Bharat
Bharat

Reputation: 29

Sub Menu Item Command MVVM

<MenuItem Header="Flag(s)" ItemsSource="{Binding Path=LineItemFlags}" Command="{Binding AssignFollowupCommand}">
    <MenuItem.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding FlagName}">    
            </TextBlock>
        </DataTemplate>
    </MenuItem.ItemTemplate>
</MenuItem>

For the parent Item i mean menu item i have a command it's invoking i want to know how to setup command for Sub Menu item

Upvotes: 3

Views: 2024

Answers (1)

Michael L Perry
Michael L Perry

Reputation: 7435

Rather than setting the ItemTemplate, set the ItemContainerStyle. Give it a style that sets the Header and Command properties of the menu item.

<MenuItem Header="_Recent Files" ItemsSource="{Binding RecentFiles}">
    <MenuItem.ItemContainerStyle>
        <Style>
            <Setter Property="MenuItem.Header" Value="{Binding FileName}"/>
            <Setter Property="MenuItem.Command" Value="{Binding Open}"/>
        </Style>
    </MenuItem.ItemContainerStyle>
</MenuItem>

Full example and description on Code Project.

Upvotes: 3

Related Questions