WPF Pass MenuItem被选为ObjectDataProvider的MethodParameter

时间:2010-03-10 15:51:55

标签: wpf menuitem objectdataprovider

我正在尝试将Selected MenuItem的Text / Header字符串作为MethodParameter传递给我的ObjectDataProvider。我在互联网上看到过类似这样的例子但却无法专门对菜单控件进行调整。我是WPF的新手,需要一些帮助来实现这一目标。任何帮助将不胜感激。

下面是代码片段,ObjectDataProvider的XAML

<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

<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>

1 个答案:

答案 0 :(得分:0)

您需要做的是绑定Header属性,而不是IsChecked。我假设您只想在检查项目时执行此操作。虽然使用MenuItem的Style是可行的,但我会主张在ViewModel中进行这种工作。

您的VM不会拥有ObjectDataProvider,而是会为每个可检查的菜单项公开布尔属性。当任何这些属性发生更改时,它可以调用该方法本身,并将该对象公开为只读属性。只需将整个控件的DataContext设置为VM的实例,绑定即可。

像这样:

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");
        }
    }
}

你的MenuItem看起来像这样:

<MenuItem Header="SNYC12P10650" IsCheckable="True" 
     ToolTip="Production" IsChecked="{Binding IsSNYC12P10650}" />