Wpf-无法将MenuItem转换为Listbox? [英] Wpf- Unable to cast MenuItem to Listbox?
问题描述
我得到例外:
'set connectionId抛出一个异常'行号'26'和行位置'34' 。
当我看到内部异常我得到:
无法转换类型为System.Windows.Controls.MenuItem的对象键入System.Windows.Controls.ListBox。
我将此例中的原因缩小到此 TreeView
MenuItem >:
< TreeView x:Name =ProjectElementTreeViewItemsSource ={Binding ProjectElementCollection}DisplayMemberPath =Name =0SelectedItemChanged =ProjectElementTreeView_SelectedItemChangedGotKeyboardFocus =ProjectElementTreeView_GotKeyboardFocus>
< TreeView.Resources>
< Style TargetType ={x:Type TreeViewItem}>
< Setter Property =HorizontalAlignmentValue =Left/>
< Setter Property =ContextMenu>
< Setter.Value>
< ContextMenu>
< MenuItem Name =AddProjectElementMenuItemHeader =AddClick =AddProjectElementMenuItem_Click/>
< / ContextMenu>
< /Setter.Value>
< / Setter>
< / Style>
< /TreeView.Resources>
< / TreeView>
仅在 MenuItem
有一个点击事件处理程序,即使点击事件处理程序不包含任何代码也会抛出。
我有同样的例外你做了看到更接近代码之后,这感觉就像一个你会得到
不能指定事件'点击'
在Style中的Target标签上,使用
EventSetter。
我不知道为什么
无论如何,使用EventSetter可以使用
< Setter.Value>
< ContextMenu>
< MenuItem Name =AddProjectElementMenuItemHeader =Add>
< MenuItem.Style>
< Style TargetType =MenuItem>
< EventSetter Event =点击Handler =AddProjectElementMenuItem_Click/>
< / Style>
< /MenuItem.Style>
< / MenuItem>
< / ContextMenu>
< /Setter.Value>
< / Setter>
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.
I got the same exception as you did. After looking closer at the code, this feels like a situation where you would get
"The event 'Click' cannot be specified on a Target tag in a Style. Use an EventSetter instead."
I'm not sure why that doesn't apply here.
Anyway, using an EventSetter works
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Name="AddProjectElementMenuItem" Header="Add">
<MenuItem.Style>
<Style TargetType="MenuItem">
<EventSetter Event="Click" Handler="AddProjectElementMenuItem_Click"/>
</Style>
</MenuItem.Style>
</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
这篇关于Wpf-无法将MenuItem转换为Listbox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!