hmmm我試圖在一個小測試項目上重現你的問題,但對我來說它的工作原理...所以我想你必須提供更多關於你的userControls如何構建的細節。下面是我的什麼作品:
我創建了一個空項目(只是應用程序和窗口文件,像往常一樣),併成立了一個網格,兩列在我的窗口:
<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類:
<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
; using 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();
}
}
}
它基本上是一個帶有「Caption」dp的文本框。在後面我窗口的代碼
現在:
public MainWindow()
{
InitializeComponent();
MyTextBox tb = new MyTextBox { Caption = "_Foo", Width = 100 };
Label lb = new Label { Content = tb.Caption + ":", Target = tb };
MyGrid.Children.Add(lb);
MyGrid.Children.Add(tb);
Grid.SetColumn(lb, 0);
Grid.SetColumn(tb, 1);
}
以及與此,我得到重點TB當我按ALT + F(我甚至可以看到_下的「富」的F-在標籤時只需按下ALT)
所以我想您的問題與您的用戶控件做自己和他們是如何構建的(什麼模板實例)
編輯:
如果您的控件未擴展現有控件,而是包含 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: 我有一個問題,一旦,將焦點轉移到dataGridCell中的子控件,這裏是我在模板中所做的:
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="TextBoxPart" Property="FocusManager.FocusedElement" Value="{Binding ElementName=TextBoxPart}" />
</Trigger>
</ControlTemplate.Triggers>
你可以嘗試添加這個到你的模板。這應該轉移你的注意力。
爲輔助功能,我不認爲這會有所幫助,但我不明白你想要什麼實現的任何方式: -/
我會使用Decorator模式,如它將允許你'包裝'XAML中的控件(即不必創建自定義控件)。我將在下班後的今天晚些時候發佈一個例子。 – Dennis 2012-03-26 13:06:48