动态构建 RIBBON:e.OriginalSource 不再是类型:RibbonButton

发布于 2024-11-28 21:39:28 字数 967 浏览 1 评论 0原文

我想动态构建我的功能区按钮。每次单击功能区按钮都必须打开用户控件。附加到按钮的命令对于所有按钮都是相同的。 在执行的过程中,我需要激活该操作的 RibbonButton。

“执行的”命令如下所示:

    private void ExecTmp(object sender, ExecutedRoutedEventArgs e)
    {
        RibbonButton btn = e.OriginalSource as RibbonButton;
        Console.WriteLine("===========e.Orig: " + e.OriginalSource.ToString());
        // do something with 'btn'   
     }

现在,奇怪的是,只要焦点不离开功能区,它就可以工作。 如果我在选项卡(文本块或其他内容)上输入用户控件,然后再次单击功能区按钮,我将无法再访问功能区按钮。我可以在控制台上看到,原因是 e.originalSource 不再是 RibbonButton 而是 textBox。控制台上的输出如下所示:

===========e.Orig: Microsoft.Windows.Controls.Ribbon.RibbonButton
===========e.Orig: System.Windows.Controls.TextBox: 0
===========e.Orig: System.Windows.Controls.TextBox: 0
===========e.Orig: System.Windows.Controls.TextBox: 0

在调试器中,我可以看到,如果打开 tabControl,单击 tabControl 中的组合框,然后单击 RibbonButton,e.Source 指向打开的 tabControl,e.OriginalSource 指向 ComboBox 。这不奇怪吗?

有人可以解释一下吗?

I want to build my RIBBON-buttons dynamically. Every click on a RIBBON-button must result in opening an usercontrol. The command attached to the button is the same for all buttons.
In the executed-procedure I need the RibbonButton that activated the action.

the "executed" Command looks like this:

    private void ExecTmp(object sender, ExecutedRoutedEventArgs e)
    {
        RibbonButton btn = e.OriginalSource as RibbonButton;
        Console.WriteLine("===========e.Orig: " + e.OriginalSource.ToString());
        // do something with 'btn'   
     }

Now, the strange thing is that it works, as long as focus does not leave the Ribbon.
If I enter a userControl on a tab (a textBlock or something) and then click on a ribbonButton again, I no longer have access to the RibbonButton. I can see on my console, that the reason for this is that e.originalSource is no longer a RibbonButton but a textBox. Output on my console looks like this:

===========e.Orig: Microsoft.Windows.Controls.Ribbon.RibbonButton
===========e.Orig: System.Windows.Controls.TextBox: 0
===========e.Orig: System.Windows.Controls.TextBox: 0
===========e.Orig: System.Windows.Controls.TextBox: 0

In the debugger I can see that if I open a tabControl, click on a combobox in the tabControl and then on the RibbonButton, e.Source points to the opened tabControl and e.OriginalSource points to the ComboBox. Isn't this strange?

Can somebody explain this ?

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

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

发布评论

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

评论(1

与之呼应 2024-12-05 21:39:28

我回答这个问题有点晚了:)但无论如何,希望我的答案对其他人有用。

为功能区控件设置 FocusManager.IsFocusScope="False"。这将导致 e.OriginalSource 包含 RibbonButton,而不是 TextBox

I'm a bit late for this question :) But anyway, hope my answer will be useful for someone else.

Set FocusManager.IsFocusScope="False" for Ribbon control. This will cause e.OriginalSource to contain RibbonButton, not TextBox.

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