VS2019创建控件事件后Control Event Manager不显示问题求助
我之前也碰到过VS WPF设计器这种莫名其妙的小bug,结合你的描述和给出的XAML代码,给你几个实用的排查和解决方向:
清理项目缓存文件
VS的缓存有时候会“抽风”,损坏的缓存文件会导致设计器的各种异常。你可以按这个步骤来操作:- 关闭VS,找到项目目录下的
.vs文件夹(默认是隐藏状态,需要先开启显示隐藏文件),直接整个删掉 - 删掉项目根目录下的
bin和obj文件夹 - 重新打开VS,依次点击菜单里的「Build -> Clean Solution」清理解决方案,再点击「Build -> Rebuild Solution」重新生成
- 关闭VS,找到项目目录下的
检查XAML绑定与后台代码的一致性
从你提供的XAML来看,两个DataGrid都绑定了同一个InventoryMouseDown事件,虽然这种写法本身是合法的,但有时候设计器会对重复绑定的事件敏感。你可以临时把其中一个DataGrid的事件改成别的名字(比如InventoryMouseDown_2),然后看Control Event Manager是否能正常显示。另外还要确认后台代码的命名空间和XAML里的x:Class="ApPG.MainWindow"完全匹配,没有拼写错误。重置VS的设置
如果上面的方法都没用,可能是VS的本地设置出了问题。你可以打开VS的菜单「Tools -> Import and Export Settings -> Reset all settings」,先备份自己的个性化设置,然后选择重置到默认设置,重启VS后再打开项目试试。排查第三方扩展的干扰
有些第三方VS扩展可能会和设计器冲突。你可以尝试用安全模式启动VS:打开命令提示符,运行devenv.exe /safemode,然后在安全模式下打开你的项目,如果Control Event Manager恢复正常,那说明是某个扩展的问题,你可以逐个禁用最近安装的扩展来找到冲突的那个。
你提供的XAML代码:
<Window x:Class="ApPG.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:ApPG" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid x:Name="ControlsGrid" Background="Black"> <Grid.RowDefinitions> <RowDefinition Height="0.5*"/> <RowDefinition Height="0.5*"/> </Grid.RowDefinitions> <TextBox x:Name="tboxdebug" HorizontalAlignment="Left" Margin="658,10,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Height="18"/> <ComboBox x:Name="comboboxdebug" HorizontalAlignment="Left" Margin="658,33,0,0" VerticalAlignment="Top" Width="120" Height="22" /> <Button x:Name="Btn_debug" Content="FNC1" HorizontalAlignment="Left" Margin="678,60,0,0" VerticalAlignment="Top" Click="Btn_debug_Click" Height="20" Width="64"/> <Button x:Name="Btn_debug2" Content="FNC2" HorizontalAlignment="Left" Margin="663,207,0,0" VerticalAlignment="Top" Click="Btn_debug2_Click" Height="20" Width="47" Grid.RowSpan="2"/> <DataGrid x:Name="pldatagrid" Margin="0,0,150,0" Grid.Row="0" Grid.Column="0" PreviewMouseDown="InventoryMouseDown" /> <DataGrid x:Name="otherdatagrid" Margin="0,0,150,0" Grid.Row="1" Grid.Column="0" PreviewMouseDown="InventoryMouseDown" /> </Grid> </Grid> </Window>
内容的提问来源于stack exchange,提问作者Ridden Dev3D

