WPF 将选择的 MenuItem 作为 MethodParameter 传递给 ObjectDataProvider

发布于 2024-08-24 19:06:31 字数 2165 浏览 11 评论 0原文

我试图将选定的菜单项的文本/标题字符串作为 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回心转意 2024-08-31 19:06:31

您需要做的是绑定 Header 属性,而不是 IsChecked。我假设您只想在检查该项目时执行此操作。虽然通过使用 MenuItem 的样式可以实现这一点,但我建议在 ViewModel 中进行此类工作。

您的虚拟机将公开每个可检查菜单项的布尔属性,而不是使用 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");
        }
    }
}

你的 MenuItems 看起来像这样:

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

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}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文