views:

423

answers:

1

I am trying to pass Selected MenuItem's Text/Header string as the MethodParameter to my ObjectDataProvider. I have seen examples like these on the internet but haven't been able to adapt it to the Menu Control specifically. I am new to WPF and need some help accomplish this. Any help would be greatly appreciated.

Below is the code snippet, XAML for the ObjectDataProvider

<Window.Resources>
    <ObjectDataProvider x:Key="NMInfo" ObjectType="{x:Type local:NMInfoProvider}" MethodName="GetDcmsInfomation" IsAsynchronous="True">
        <ObjectDataProvider.MethodParameters>
            <x:Static Member="system:String.Empty" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

XAML for the Menu control

<Menu Name="nmMenu" Height="25" HorizontalAlignment="Stretch" VerticalAlignment="Top" FontSize="12" DockPanel.Dock="Top">

        <Menu.BitmapEffect>
            <DropShadowBitmapEffect/>
        </Menu.BitmapEffect>
        <MenuItem Header="File">

            <MenuItem Header="SNYC12P10650" IsCheckable="True" ToolTip="Production" Click="MenuItem_Clicked">
                <MenuItem.IsChecked>
                    <Binding Source="{StaticResource NMInfo}" Path="MethodParameters[0]" BindsDirectlyToSource="True" Mode="OneWayToSource"/>
                </MenuItem.IsChecked>
            </MenuItem>
            <MenuItem Header="GPRI12D10217" IsCheckable="True" ToolTip="QA" Click="MenuItem_Clicked">
               <MenuItem.IsChecked>
                    <Binding Source="{StaticResource NMInfo}" Path="MethodParameters[0]" BindsDirectlyToSource="True" Mode="OneWayToSource"/>
                </MenuItem.IsChecked>
            </MenuItem>
            <MenuItem Header="GPRI12D10219" IsCheckable="True" ToolTip="Dev" Click="MenuItem_Clicked">
                <MenuItem.IsChecked>
                    <Binding Source="{StaticResource NMInfo}" Path="MethodParameters[0]" BindsDirectlyToSource="True" Mode="OneWayToSource"/>
                </MenuItem.IsChecked>
            </MenuItem>
            <Separator/>
            <MenuItem Header="Close"/>
        </MenuItem>

    </Menu>
A: 

What you need to do is Bind the Header property, not IsChecked. I'm assuming you only want to do this when the item is checked though. While this would be feasible by using a Style for the MenuItem, I would advocate doing this sort of work in a ViewModel.

Instead of having an ObjectDataProvider, your VM would expose boolean properties for each of the checkable menu items. When any of these properties changed, it could call that method itself, and expose the object as a read-only property. Just set the DataContext of the whole control to an instance of your VM, and the bindings would work.

Something like so:

public class NMInfoViewModel : INotifyPropertyChanged
{
    private bool isSNYC12P10650 = false;
    public bool IsSNYC12P10650
    {
        get { return isSNYC12P10650; }
        set
        {
            if (value == isSNYC12P10650) return;
            isSNYC12P10650 = value;
            OnPropertyChanged("IsSNYC12P10650");

            if (value)
                NMInfo = NMInfoProvider.GetDcmsInfomation("SNYC12P10650");
        }
    }

    ...

    private NMInfo nMInfo;
    public NMInfo NMInfo
    {
        get { return nMInfo; }
        private set
        {
            if (value == nMInfo) return;
            nMInfo = value;
            OnPropertyChanged("NMInfo");
        }
    }
}

And your MenuItems would look like this:

<MenuItem Header="SNYC12P10650" IsCheckable="True" 
     ToolTip="Production" IsChecked="{Binding IsSNYC12P10650}" />
Abe Heidebrecht
Hello Abe, I appreciate your help. Can you please let me know how to use the Style on MenuItem to achieve the same as well?Thanks,Shravan
Shravan