动态构建 RIBBON:e.OriginalSource 不再是类型:RibbonButton
我想动态构建我的功能区按钮。每次单击功能区按钮都必须打开用户控件。附加到按钮的命令对于所有按钮都是相同的。 在执行的过程中,我需要激活该操作的 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我回答这个问题有点晚了:)但无论如何,希望我的答案对其他人有用。
为功能区控件设置
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 causee.OriginalSource
to containRibbonButton
, notTextBox
.