C# 移除WPF中RibbonMenuItem的焦点
我的WPF应用程序中有一个简单的Ribbon,其中包含RibbonGroup和RibbonMenuButton。单击此按钮后,我想删除焦点。现在我不得不双击或点击其他东西来失去焦点C# 移除WPF中RibbonMenuItem的焦点,c#,wpf,focus,ribbon,C#,Wpf,Focus,Ribbon,我的WPF应用程序中有一个简单的Ribbon,其中包含RibbonGroup和RibbonMenuButton。单击此按钮后,我想删除焦点。现在我不得不双击或点击其他东西来失去焦点 <Custom:Ribbon Name="RibbonMenu" Focusable="True" Background="WhiteSmoke" HorizontalAlignment="Stretch" VerticalAlignment="Top" Loaded="RibbonLoaded" Grid.R
<Custom:Ribbon Name="RibbonMenu" Focusable="True" Background="WhiteSmoke" HorizontalAlignment="Stretch" VerticalAlignment="Top" Loaded="RibbonLoaded" Grid.Row="0" Grid.ColumnSpan="2">
<Custom:RibbonTab Name="home" Header="Home" KeyTip="H" Focusable="True" >
<Custom:RibbonGroup Focusable="True" x:Name="utilsGroup" Header="Utils">
<Custom:RibbonMenuButton x:Name="btUtils1" PreviewMouseLeftButtonDown="btUtils1_MouseLeftButtonDown" Label="Utility no 1" LargeImageSource="Resources/utils.png"/>
</Custom:RibbonGroup>
</Custom:RibbonTab>
</Custom:Ribbon>
根本不起作用。我在寻找解决办法,但似乎网络上的任何东西都能帮我。有什么建议吗?如果没有一个好的,特别是更精确的解释,说明你实际上想要代码做什么,也就是说,清楚准确地说明代码现在做什么,以及你想要它做什么,你很难确定你想要完成什么
也就是说,在您给出的示例中,您有一个RibbonMenuButton
,当单击该按钮时,会打开一个下拉列表,该下拉列表将一直保持打开状态,直到鼠标捕捉丢失为止(即,用户使用鼠标或键盘将注意力引向其他位置)
如果您特别想在单击时使此下拉列表消失,可以通过将IsDropDownOpen
属性设置为false
来完成此操作。请注意,您必须在所有输入处理完成后执行此操作,因为是输入本身导致下拉列表打开。为此,可以使用Dispatcher.InvokeAsync()
。例如:
private void btUtils1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Dispatcher.InvokeAsync(() => btUtils1.IsDropDownOpen = false);
}
也就是说,你确定你真的想要一个
RibbonMenuButton
?如果您不希望控件保持打开状态,或者不希望单击会明显影响控件,那么您真正想要的可能是RibbonButton
。该控件类型的行为类似于普通按钮,单击后不会产生任何可见的后效
private void btUtils1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Dispatcher.InvokeAsync(() => btUtils1.IsDropDownOpen = false);
}