Wpf-无法将 MenuItem 转换为列表框?
我遇到了一个非常奇怪的异常。 我收到异常:
“'设置 connectionId 引发异常。'行号“26”和行位置“34”。”
当我查看内部异常时,我得到:
“无法将类型为“System.Windows.Controls.MenuItem”的对象转换为类型“System” .Windows.Controls.ListBox'."
我已将异常原因缩小到此 TreeView
中包含的 TreeViewItem 样式中的 MenuItem
:
<TreeView x:Name="ProjectElementTreeView" ItemsSource="{Binding ProjectElementCollection}" DisplayMemberPath="Name" Padding="0" SelectedItemChanged="ProjectElementTreeView_SelectedItemChanged" GotKeyboardFocus="ProjectElementTreeView_GotKeyboardFocus">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Name="AddProjectElementMenuItem" Header="Add" Click="AddProjectElementMenuItem_Click"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
仅异常当 MenuItem
具有单击事件处理程序时发生,即使单击事件处理程序不包含任何代码也会引发该异常。
I am getting a very strange exception.
I get the exception:
"'Set connectionId threw an exception.' Line number '26' and line position '34'."
When I look at the Inner Exception I get:
"Unable to cast object of type 'System.Windows.Controls.MenuItem' to type 'System.Windows.Controls.ListBox'."
I have narrowed the cause of the exception to the MenuItem
in the TreeViewItem style contained in this TreeView
:
<TreeView x:Name="ProjectElementTreeView" ItemsSource="{Binding ProjectElementCollection}" DisplayMemberPath="Name" Padding="0" SelectedItemChanged="ProjectElementTreeView_SelectedItemChanged" GotKeyboardFocus="ProjectElementTreeView_GotKeyboardFocus">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Name="AddProjectElementMenuItem" Header="Add" Click="AddProjectElementMenuItem_Click"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
The exception only occurs when the MenuItem
has a click event handler and is thrown even when the click event handler does not contain any code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到了和你一样的异常。仔细查看代码后,感觉就像您会得到的情况
我不知道为什么这不适用于这里。
无论如何,使用 EventSetter 是有效的
I got the same exception as you did. After looking closer at the code, this feels like a situation where you would get
I'm not sure why that doesn't apply here.
Anyway, using an EventSetter works
我不得不亲自面对这种奇怪的情况。
有一个简单的方法可以克服它,您必须清理并重建项目,异常就会消失。
希望这有帮助。
I had to face this weird situation myself.
There is an easy way to get over it, you have to clean and rebuild the project and the exception will go away.
Hope this helps.
我已经复制了你的代码,它对我有用。您确定您发布的代码导致了问题吗?
不要将 MenuItem 放置在 ContextMenu 的内容中,而是将其嵌套在 ContextMenu.Items 下:
I've copied your code and it works for me. Are you sure the code you posted is causing the problem?
Instead of placing the MenuItem in the content of ContextMenu, nest it under ContextMenu.Items:
我遇到了这个错误,发现我将标记放在
之外,我的意思是我知道它必须在其中,但是我没有注意到结束标签已经在我的上下文菜单标签之前被调用。我以为我还在里面。我只是将其移到结束标签之前,并且它按预期工作。I ran into this error and found out that I put the tagging outside
<MyApp:AppPage.Resources></MyApp:AppPage.Resources>
, i mean I know it has to be inside it but I did not notice that the closing tag was already called before my context menu tag. I thought I was still inside it. I just moved it before the closing tag and it worked as expected.我认为,Fredrik 的回答解释了实际导致此问题的原因。但这里有一个替代解决方案。
您还可以通过将
ContextMenu
声明为Style
之外的单独资源来解决该问题:Fredrik's answer explains what's actually causing this, I think. But here's an alternate solution.
You can also work around the problem by declaring the
ContextMenu
as a separate resource outside of theStyle
: