Wpf- Unable to cast MenuItem to Listbox?

2020-06-19 09:59发布

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.

5条回答
该账号已被封号
2楼-- · 2020-06-19 10:24

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.

查看更多
Animai°情兽
3楼-- · 2020-06-19 10:35

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:

<ListBox.ContextMenu>
    <ContextMenu>
        <ContextMenu.Items>
            <MenuItem Name="AddProjectElementMenuItem"></MenuItem>
        </ContextMenu.Items>
    </ContextMenu>
</ListBox.ContextMenu>a
查看更多
啃猪蹄的小仙女
4楼-- · 2020-06-19 10:37

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>
查看更多
聊天终结者
5楼-- · 2020-06-19 10:41

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 the Style:

<TreeView x:Name="ProjectElementTreeView" ItemsSource="{Binding ProjectElementCollection}"  DisplayMemberPath="Name" Padding="0" SelectedItemChanged="ProjectElementTreeView_SelectedItemChanged" GotKeyboardFocus="ProjectElementTreeView_GotKeyboardFocus">
    <TreeView.Resources>
        <ContextMenu x:Key="Menu">
            <MenuItem Name="AddProjectElementMenuItem" Header="Add"
                      Click="AddProjectElementMenuItem_Click"/>
        </ContextMenu>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="ContextMenu" Value="{StaticResource Menu}" />
        </Style>
    </TreeView.Resources>
</TreeView>
查看更多
一夜七次
6楼-- · 2020-06-19 10:47

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.

查看更多
登录 后发表回答