简体   繁体   English

菜单项绑定WPF问题

[英]Menu Item Binding WPF Issue

I have a problem with Menu Item Command Binding. 菜单项命令绑定有问题。 I have used MVVM pattern When I use right click, the menu appears. 我使用了MVVM模式当我使用右键单击时,会出现菜单。 But when I click on the menu item doesn't work. 但是当我点击菜单项时不起作用。 Do you know why? 你知道为什么吗? Thanks 谢谢

Here is the XAML: 这是XAML:

    <UserControl x:Class="PlotView.ViewModel.PlotViewControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:oxy="http://oxyplot.org/wpf"
             mc:Ignorable="d" 
             xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
             d:DesignHeight="400" d:DesignWidth="600"
             x:Name="theViewName">

    <UserControl.Resources>
    </UserControl.Resources>

    <GroupBox  Name="GB" Header="Graphs"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  BorderThickness="0">
        <ListView Name="PlotLista"    SelectedIndex="{Binding SelectedValue}" ItemsSource="{Binding PlotModelList}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <oxy:Plot  MinHeight="260" Height="Auto"   IsRendering="True" FontStyle="Normal" FontFamily="Arial" FontSize="8"  VerticalContentAlignment="Top"  HorizontalContentAlignment="Left" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=ActualWidth}"  Model="{Binding }">
                        <oxy:Plot.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Export to PNG" Command="{Binding DataContext.SavePNG, ElementName=theViewName}"/>
                            </ContextMenu>
                        </oxy:Plot.ContextMenu>
                    </oxy:Plot>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </GroupBox>
</UserControl>

Here are a small portion of ViewModel: 以下是ViewModel的一小部分:

 #region Fields
        private readonly DelegateCommand _menuClick=new DelegateCommand(this.MenuItemClick);
  #endregion

  #region Command
 public ICommand SavePNG
 {
   get { return this._menuClick; }
 }
  #endregion

 private void MenuItemClick()
{
   // some code here
}

Error: 错误:

System.Windows.Data Error: 40 : BindingExpression path error: 'SavePNG' property not found on 'object' ''PlotModel' (HashCode=15385318)'. System.Windows.Data错误:40:BindingExpression路径错误:'object'''PlotModel'(HashCode = 15385318)'上找不到'SavePNG'属性。 BindingExpression:Path=SavePNG; BindingExpression:路径= SavePNG; DataItem='PlotModel' (HashCode=15385318); DataItem ='PlotModel'(HashCode = 15385318); target element is 'MenuItem' (Name=''); target元素是'MenuItem'(Name =''); target property is 'Command' (type 'ICommand') target属性是'Command'(类型'ICommand')

Your binding is trying to find SavePNG on your Item , not your ViewModel. 您的绑定试图在您的项目上找到SavePNG ,而不是您的ViewModel。

Instead, give your view an x:Name, or a Name, and use the following binding instead: 相反,为视图提供x:Name或Name,并使用以下绑定:

{Binding DataContext.SavePNG, ElementName=theViewName}

Assuming that SaveCommand is on the same ViewModel which contains your collection. 假设SaveCommand位于包含您的集合的同一ViewModel上。

ContextMenus are a little different in wpf as they are not part of the visual tree of the control. ContextMenus在wpf中略有不同,因为它们不是控件的可视树的一部分。 As such they cannot 'see' any elements by relative source or by elementnames. 因此,他们无法通过相对来源或元素名称“看到”任何元素。

A little cheat is to use the PlacementTarget property and get the datacontext with that. 有点作弊是使用PlacementTarget属性并使用它获取datacontext。 Change your ListView to below to make it work. 将ListView更改为以下以使其工作。 Notice the tag property on the ListView and the DataContext property on the ContextMenu. 注意ListView上的tag属性和ContextMenu上的DataContext属性。

<ListView Name="PlotLista"    SelectedIndex="{Binding SelectedValue}" ItemsSource="{Binding PlotModelList}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Tag="{Binding DataContext,ElementName=theViewName}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <oxy:Plot  MinHeight="260" Height="Auto"   IsRendering="True" FontStyle="Normal" FontFamily="Arial" FontSize="8"  VerticalContentAlignment="Top"  HorizontalContentAlignment="Left" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=ActualWidth}"  Model="{Binding }">
                        <oxy:Plot.ContextMenu>
                            <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                                <MenuItem Header="Export to PNG" Command="{Binding SavePNG}"/>
                            </ContextMenu>
                        </oxy:Plot.ContextMenu>
                    </oxy:Plot>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

This wasn't written in visual studio so please check the syntax: 这不是在visual studio中编写的,所以请检查语法:

  <ListView  Tag="{Binding Path=DataContext,ElementName=theViewName}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <oxy:Plot Tag="{Binding Path=Tag,RelativeSource={RelativeSource AncestorType=ListView}">
                    <oxy:Plot.ContextMenu>
                        <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                            <MenuItem Command="{Binding SavePNG}"/>
                        </ContextMenu>
                    </oxy:Plot.ContextMenu>
                </oxy:Plot>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM