使用ListViewItem ContextMenu

时间:2015-08-13 16:55:08

标签: c# wpf xaml

我有一个ListView,其中包含定义的每个项目的上下文菜单:

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Foreground" Value="{Binding ListViewForeground}"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <MenuItem Header="Edit" Click="editContextMenuItem_Click"/>
                        <MenuItem Header="{Binding enable_Disable_OppositeText}" Click="enable_disable_ContextMenuItem_Click"/>
                        <MenuItem Header="Delete" Click="deleteContextMenuItem_Click"/>
                    </ContextMenu>
                </Setter.Value>
            </Setter>
            <EventSetter Event="Control.MouseDoubleClick" Handler="discreteListViewItem_MouseDoubleClick"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

后面的Click处理程序:

private void editContextMenuItem_Click(object sender, RoutedEventArgs e)
{
    Console.WriteLine("Edit");
}

private void enable_disable_ContextMenuItem_Click(object sender, RoutedEventArgs e)
{
    Console.WriteLine("Enable/Disable");
}

private void deleteContextMenuItem_Click(object sender, RoutedEventArgs e)
{
    Console.WriteLine("Delete");
}

目前,这会在构建/运行时显示.g.cs文件中的错误:

  1. }预计
  2. {预计
  3. 无效的表达式字词'}'
  4. ;预期
  5. 如果删除了3 Menu Item行,则会成功构建。清洁解决方案和重建工作没有奏效;既没有删除binobj目录也没有重建。此外,名称空间和类名称尚未更改。

1 个答案:

答案 0 :(得分:1)

我能够重现这个问题。在我的项目中,EventSetter.Handler带有下划线(VS 2015),错误:

  

属性'Handler'的值无效:'Microsoft.VisualStudio.DesignTools.Xaml.LanguageService.Semantics.XmlValue

EventSetter文档中有一些评论,但我没有花时间阅读它们。我提出了一个有效的解决方案,如下所示:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <ListView>
        <ListView.Resources>
            <Style TargetType="{x:Type ListViewItem}">
                <EventSetter Event="Control.MouseDoubleClick" Handler="discreteListViewItem_MouseDoubleClick"/>
            </Style>
        </ListView.Resources>
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
               <Setter Property="Foreground" Value="{Binding ListViewForeground}"/>
               <Setter Property="HorizontalContentAlignment" Value="Center"/>
               <Setter Property="ContextMenu">
                   <Setter.Value>
                       <ContextMenu>
                           <MenuItem Header="Edit" Click="editContextMenuItem_Click"/>
                           <MenuItem Header="{Binding enable_Disable_OppositeText}" Click="enable_disable_ContextMenuItem_Click"/>
                           <MenuItem Header="Delete" Click="deleteContextMenuItem_Click"/>
                       </ContextMenu>
                   </Setter.Value>
               </Setter>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>
</Window>