WPF 将选择的 MenuItem 作为 MethodParameter 传递给 ObjectDataProvider
我试图将选定的菜单项的文本/标题字符串作为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要做的是绑定 Header 属性,而不是 IsChecked。我假设您只想在检查该项目时执行此操作。虽然通过使用 MenuItem 的样式可以实现这一点,但我建议在 ViewModel 中进行此类工作。
您的虚拟机将公开每个可检查菜单项的布尔属性,而不是使用 ObjectDataProvider。当这些属性中的任何一个发生更改时,它可以调用该方法本身,并将对象公开为只读属性。只需将整个控件的 DataContext 设置为 VM 的实例,绑定就会起作用。
像这样:
你的 MenuItems 看起来像这样:
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:
And your MenuItems would look like this: