gpt4 book ai didi

c# - 带有 ListViewItem ContextMenu 的 g.cs 文件出错

转载 作者:太空宇宙 更新时间:2023-11-03 13:02:12 25 4
gpt4 key购买 nike

我有一个 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. >;预计

如果删除了 3 个 Menu Item 行,则构建成功。清理解决方案并重建没有用;也没有删除 binobj 目录并重建。此外,命名空间和类名也没有更改。

最佳答案

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

Invalid value for property '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>

关于c# - 带有 ListViewItem ContextMenu 的 g.cs 文件出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31994221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com