Wpf-无法将 MenuItem 转换为列表框?

发布于 2024-10-08 13:01:17 字数 1321 浏览 2 评论 0原文

我遇到了一个非常奇怪的异常。 我收到异常:

“'设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡淡的优雅 2024-10-15 13:01:17

我遇到了和你一样的异常。仔细查看代码后,感觉就像您会得到的情况

“无法指定事件‘Click’
在样式中的目标标签上。使用
相反,EventSetter。”

我不知道为什么这不适用于这里。
无论如何,使用 EventSetter 是有效的

<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>

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>
抱猫软卧 2024-10-15 13:01:17

我不得不亲自面对这种奇怪的情况。
有一个简单的方法可以克服它,您必须清理并重建项目,异常就会消失。


希望这有帮助。

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.

书间行客 2024-10-15 13:01:17

我已经复制了你的代码,它对我有用。您确定您发布的代码导致了问题吗?

不要将 MenuItem 放置在 ContextMenu 的内容中,而是将其嵌套在 ContextMenu.Items 下:

<ListBox.ContextMenu>
    <ContextMenu>
        <ContextMenu.Items>
            <MenuItem Name="AddProjectElementMenuItem"></MenuItem>
        </ContextMenu.Items>
    </ContextMenu>
</ListBox.ContextMenu>a

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
感情旳空白 2024-10-15 13:01:17

我遇到了这个错误,发现我将标记放在 之外,我的意思是我知道它必须在其中,但是我没有注意到结束标签已经在我的上下文菜单标签之前被调用。我以为我还在里面。我只是将其移到结束标签之前,并且它按预期工作。

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.

青衫负雪 2024-10-15 13:01:17

我认为,Fredrik 的回答解释了实际导致此问题的原因。但这里有一个替代解决方案。

您还可以通过将 ContextMenu 声明为 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文