从 Excel 加载项项目打开窗口时,WPF 菜单在鼠标松开时关闭

发布于 2024-11-25 08:11:38 字数 1117 浏览 0 评论 0原文

我在 WPF 中创建菜单时遇到问题。发生的情况是,当您停止按下鼠标按钮时,它会自动关闭。我希望它的行为就像常规菜单一样,您可以单击其中,子项目将保持不变,但我无论如何都找不到完成此操作的方法。

代码如下所示:

<Window x:Class="ExcelAddIn.MyWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
        <Grid>
            <DockPanel>
                <Menu Width="Auto" IsMainMenu="True" >
                    <MenuItem Header="Item">
                        <MenuItem Header="SubItem" />
                    </MenuItem>
                </Menu>
            </DockPanel>
        </Grid>
</Window>

我想知道它是否与逻辑焦点有关?我看到一些关于它可能是 .NET 框架中的错误的信息?有什么想法吗?

提前致谢,

一开始我认为这没有什么区别,但显然确实有什么区别。当在独立的 WPF 应用程序中运行代码时,它可以工作,但是当我尝试从 Excel-addin 项目打开 WPF 窗口时,我遇到了这个问题。

I have a problem when creating a menu in WPF. What happens is that it closes automatically when you stop pressing the mouse button. I want it to behave as regular menu's where you can click and the subitems will stay up but I can't find anyway to get this done.

The code looks like this:

<Window x:Class="ExcelAddIn.MyWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
        <Grid>
            <DockPanel>
                <Menu Width="Auto" IsMainMenu="True" >
                    <MenuItem Header="Item">
                        <MenuItem Header="SubItem" />
                    </MenuItem>
                </Menu>
            </DockPanel>
        </Grid>
</Window>

I'm wondering if it has anything to do with logical focus maybe? I saw something about it might being a bug in .NET framwork? Any ideas?

Thanks in advance

I didn't think it made any difference at first but obviously it does. When running the code in a standalone WPF application it works, however when I try to open the WPF window from a Excel-addin project I get this problem..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

翻身的咸鱼 2024-12-02 08:11:38

好的!我解决了这个问题。事实证明,这毕竟是一个焦点问题。
当 Excel 插件执行 WPF 窗口时,Excel 窗口仍然处于焦点状态。因此,每次鼠标按下时,焦点都会从 WPF 跳回 excel。

我所要做的就是将执行方式从: 更改

MainWindow mainWindow = new MainWindow();
mainWindow.Activate();
mainWindow.Show();

为: 无论如何,

MainWindow mainWindow = new MainWindow();
mainWindow.Activate();
mainWindow.ShowDialog();

谢谢你们的帮助!

Ok! I solved the problem. Turns out it was a focus problem after all.
When the excel addin executed the WPF window the excel window was still in focus. So on every mouseup the focus would jump back from WPF to excel.

All I had to do was change the execution from this:

MainWindow mainWindow = new MainWindow();
mainWindow.Activate();
mainWindow.Show();

to this:

MainWindow mainWindow = new MainWindow();
mainWindow.Activate();
mainWindow.ShowDialog();

Thanks for help anyway guys!

眼泪都笑了 2024-12-02 08:11:38

我稍微调整了您的代码(DockPanel.Dock="Top" 和一个额外的网格来填充停靠面板的其余部分)。它工作正常并且菜单保持打开状态。它对你有用吗?:

<Window x:Class="WpfApplication2.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window3" Height="300" Width="300">
    <Grid>
        <DockPanel>
            <Menu Width="Auto"
                  IsMainMenu="True" DockPanel.Dock="Top">
                <MenuItem Header="Item">
                    <MenuItem Header="SubItem" />
                </MenuItem>
            </Menu>
            <Grid />
        </DockPanel>
    </Grid>
</Window>

I adjusted your code a tiny bit (DockPanel.Dock="Top" and an extra grid to fill the rest of the dock panel). It works fine and the menu stays open. Does it work for you?:

<Window x:Class="WpfApplication2.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window3" Height="300" Width="300">
    <Grid>
        <DockPanel>
            <Menu Width="Auto"
                  IsMainMenu="True" DockPanel.Dock="Top">
                <MenuItem Header="Item">
                    <MenuItem Header="SubItem" />
                </MenuItem>
            </Menu>
            <Grid />
        </DockPanel>
    </Grid>
</Window>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文