Kiddo
Kiddo

Reputation: 5082

Dynamically bound menu

i'm noobie on WPF.
i have this Admin menu include 'manage A','manage B','manage C'

in my XAML

<MenuItem Header="_Admin" Name="adminMenuItem" Visibility="{Binding Path=IsAdmin, Mode=OneWay,}" >

 <MenuItem Header="manage A" Command="ShowTab" />
 <MenuItem Header="manage B" Command="ShowTab" />
 <MenuItem Header="manage C" Command="ShowTab" />            

</MenuItem>

in my mainWindow.cs code,

private void ShowTab(MenuItem menuItem)
{
    if (menuItem.Header = "manage A")
        showTabA();
    if (menuItem.Header = "manage B")
        showTabB();
    if (menuItem.Header = "manage C")
        showTabC();
}

can i bind menuitem with commands like that? if not, what's the best way to get the value from different menu items.

Many thanks

Upvotes: 1

Views: 102

Answers (1)

brunnerh
brunnerh

Reputation: 185445

Specify a CommandParameter in the MenuItems which identifies the tab, and get that value from the ExecutedRoutedEventArgs.Parameter property, it's cleaner than using the header at the very least.

Upvotes: 1

Related Questions