菜单项绑定WPF问题

本文关键字:问题 WPF 绑定 菜单项 | 更新日期: 2023-09-27 17:50:42

我有一个菜单项命令绑定的问题。我使用了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。数据错误:40:BindingExpression路径错误在"对象"PlotModel"上找不到"SavePNG"属性(HashCode = 15385318)"。BindingExpression:路径= SavePNG;DataItem = ' PlotModel ' (HashCode = 15385318);目标元素是"菜单项"。(Name = ");目标属性为"Command"(类型为"iccommand")

菜单项绑定WPF问题

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

相反,给你的视图一个x:Name,或者一个Name,并使用下面的绑定:

{Binding DataContext.SavePNG, ElementName=theViewName}

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

上下文菜单在wpf中略有不同,因为它们不是控件可视树的一部分。因此,它们不能通过相对源或元素名称"看到"任何元素。

一个小窍门是使用PlacementTarget属性并获取数据上下文。将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>

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