菜单项绑定WPF问题

时间:2015-05-26 08:48:26

标签: c# wpf mvvm

菜单项命令绑定有问题。我使用过MVVM模式 当我使用右键单击时,会出现菜单。但是当我点击菜单项时不起作用。你知道为什么吗?感谢

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

以下是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
}

错误:

  

System.Windows.Data错误:40:BindingExpression路径错误:   &#39; SavePNG&#39;在&#39; object&#39;上找不到的属性&#39;&#39; PlotModel&#39;   (的HashCode = 15385318)&#39 ;. BindingExpression:路径= SavePNG;   的DataItem =&#39; PlotModel&#39; (的HashCode = 15385318);目标元素是&#39; MenuItem&#39;   (名称=&#39;&#39);目标财产是&#39; Command&#39; (键入&#39; ICommand&#39;)

3 个答案:

答案 0 :(得分:0)

您的绑定试图在您的项目上找到SavePNG,而不是您的ViewModel。

相反,请为视图指定x:Name或Name,然后使用以下绑定:

{Binding DataContext.SavePNG, ElementName=theViewName}

答案 1 :(得分:0)

假设SaveCommand位于包含您的集合的同一ViewModel上。

ContextMenus在wpf中有点不同,因为它们不是控件的可视树的一部分。因此,他们不能看到&#39;相对来源或元素名称的任何元素。

有点作弊是使用PlacementTarget属性并使用它获取datacontext。将ListView更改为以下以使其工作。注意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>

答案 2 :(得分:0)

这不是在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>