在使用ListViewItem ContextMenu的g.cs文件中出现错误
本文关键字:文件 错误 cs ListViewItem ContextMenu | 更新日期: 2023-09-27 18:07:44
我有一个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文件中的错误:
- <
- }预期/gh><
- {预期/gh>
- 无效的表达式术语'}'
- ;将
如果3个Menu Item
行被删除,则构建成功。清洗溶液和重建都不起作用;删除bin
和obj
目录并重新构建也没有。同时,命名空间和类名也没有改变。
我能够重现这个问题。在我的项目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>