我有一个问题,我无法让 FocusManager 识别功能区控件何时具有焦点。
逻辑很简单。我把焦点放在一个文本框上。我已经尝试过 RibbonText 和 Textbox。我看到当我单击文本框时,GotFocus 事件会触发,表明它现在具有焦点。当我在画布上移动鼠标时,它应该更新文本框,但这不会发生。FocusManager.GetFocusedElement 返回 null。我用状态栏尝试了这个并添加了一个文本框。GetFocusedElement 使用状态栏返回文本框。
当我使用具有两列的网格时,此完全相同的代码有效。网格在左列有一个画布,在右列有一个带有几个扩展器控件的堆栈面板,其中包含文本框。右侧忙于太多扩展器控件,因此我切换到功能区控件,因为多个选项卡和其他功能使其成为最佳选择。
我已经尝试了设置“Focusable”的所有组合,但它没有帮助
<ribbon:RibbonWindow x:Class="WpfRibbonApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
Title="MainWindow" x:Name="RibbonWindow" Width="640" Height="480">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ribbon:Ribbon x:Name="Ribbon">
<ribbon:Ribbon.ApplicationMenu>
<ribbon:RibbonApplicationMenu SmallImageSource="Images\SmallIcon.png">
<ribbon:RibbonApplicationMenuItem Header="Hello _Ribbon" x:Name="MenuItem1" ImageSource="Images\LargeIcon.png"/>
</ribbon:RibbonApplicationMenu>
</ribbon:Ribbon.ApplicationMenu>
<ribbon:RibbonTab x:Name="HomeTab"
Header="Home">
<ribbon:RibbonGroup x:Name="Group1"
Header="Group1">
<TextBox Name="Try" Width="200" GotFocus="Try_GotFocus"></TextBox>
</ribbon:RibbonGroup>
</ribbon:RibbonTab>
</ribbon:Ribbon>
<Canvas Grid.Row="1" Height="250" Margin="0,5,0,0" Name="canvas1" Background="AliceBlue" VerticalAlignment="Top" MouseMove="canvas1_MouseMove" />
</Grid>
</ribbon:RibbonWindow>
using Microsoft.Windows.Controls.Ribbon;
namespace WpfRibbonApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : RibbonWindow
{
public MainWindow()
{
InitializeComponent();
}
private void canvas1_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
{
System.Windows.Point p = e.GetPosition(canvas1);
var fe = FocusManager.GetFocusedElement(this);
if (fe != null)
{
var element = fe as FrameworkElement;
if (element.Name == "Try")
{
Try.Text = "Canvas has Focus";
}
}
}
}
private void Try_GotFocus(object sender, RoutedEventArgs e)
{
Try.Text = "Got Focus";
}
}
}