嗯,我尝试在一个小的测试项目中重现您的问题,但对我来说它是有效的...所以我猜您需要提供更多有关如何构建您的用户控件的细节。这是对我有效的方法:
我创建了一个空项目(只有应用程序和窗口文件,像往常一样),并在我的窗口中设置了一个具有2列的网格:
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Name="Window"
SizeToContent="WidthAndHeight">
<Grid Name="MyGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
</Grid>
</Window>
然后创建了一个扩展WPF TextBox类的userControl:
<TextBox x:Class="Test.MyTextBox"
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">
</TextBox>
以及:
使用 System.Windows;
使用 System.Windows.Controls;
namespace Test
{
public partial class MyTextBox : TextBox
{
public static readonly DependencyProperty CaptionProperty =
DependencyProperty.Register("Caption", typeof(string), typeof(MyTextBox), new UIPropertyMetadata(""));
public string Caption
{
get { return (string)GetValue(CaptionProperty); }
set { SetValue(CaptionProperty, value); }
}
public MyTextBox()
{
InitializeComponent();
}
}
}
这基本上是一个带有“标题”dp的文本框。
现在在我的窗口代码后面:
public MainWindow()
;
Label lb = new Label ;
MyGrid.Children.Add(lb);
MyGrid.Children.Add(tb);
Grid.SetColumn(lb, 0);
Grid.SetColumn(tb, 1);
}
通过这种方式,当我按下ALT + F时,我确实会将焦点放在TB上(甚至可以在标签中仅按ALT时看到“Foo”的F下面的_)
所以我想你的问题与你的用户控件本身以及它们是如何构建的有关(例如使用了哪个模板)
编辑:
如果您的控件不是扩展现有控件而是包含WPF控件,则问题可能出在Focus方法上。您应该添加一个Focus()方法,在控件本身获得焦点时将焦点设置在控件的正确部分。
代码(用于包含要获取焦点的文本框的用户控件):
<TextBox x:Class="Test.MyTextBox"
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>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Content="foo" Grid.Column="0" />
<TextBox Name="TextBoxPart" Grid.Column="1" />
</Grid>
</TextBox>
代码后台
public partial class MyTextBox : TextBox
{
public static readonly DependencyProperty CaptionProperty =
DependencyProperty.Register("Caption", typeof(string), typeof(MyTextBox), new UIPropertyMetadata(""));
public string Caption
{
get { return (string)GetValue(CaptionProperty); }
set { SetValue(CaptionProperty, value); }
}
public MyTextBox()
{
InitializeComponent();
}
protected override void OnGotFocus(RoutedEventArgs e)
{
TextBoxPart.Focus();
}
}
编辑2:
我曾经遇到一个问题,需要将焦点转移到数据网格单元格中的子控件,这是我在模板中所做的:
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="TextBoxPart" Property="FocusManager.FocusedElement" Value="{Binding ElementName=TextBoxPart}" />
</Trigger>
</ControlTemplate.Triggers>
你可以尝试将这段代码添加到你的模板中。这应该能够正确地转移你的焦点。
至于可访问性,我不认为这会有所帮助,但我也没有看到任何实现你想要的方式的方法 :-/