hmmm 작은 테스트 프로젝트에서 문제를 재현하려고했지만 나에게 그것은 효과가 있습니다 ... 그래서 당신은 userControls가 어떻게 만들어 졌는지에 대해 더 자세히 설명해야 할 것입니다. (평소처럼, 그냥 응용 프로그램 및 윈도우 파일)
내가 빈 프로젝트를 만들어 내 창에서 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>
는 다음의 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>
하고 : 즉 WPF 입력란 클래스 확장
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();
}
}
}
기본적으로 "캡션"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);
}
이와
, 나는 ALT + F (심지어 "푸"의 F 아래 _ 볼 수 있습니다를 누를 때 TB에 포커스를 취득 할 라벨의 경우 누르기 ALT)
그래서 나는 당신의 문제를 생각은 당신의 UserControls와 함께 자신을 할 수있다 그리고 그들은 (내장하는 방법을 어떤 예를 들어 템플릿)
편집 :
,
컨트롤이 기존 컨트롤을 확장하지 않고 에 WPF 컨트롤이있는 경우 문제는 아마도 Focus 메서드에 있습니다. 컨트롤 자체가 포커스를 얻었을 때 컨트롤의 오른쪽 부분에 포커스를 설정하는 Focus() 메서드를 추가해야합니다.
코드는 (당신이 인스턴스에 대한 포커스를 취득하고자하는 텍스트 상자가 포함 된 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">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Content="foo" Grid.Column="0" />
<TextBox Name="TextBoxPart" Grid.Column="1" />
</Grid>
</TextBox>
코드 2 편집
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();
}
}
뒤에 : 를 내가 한 번 문제가 있었다 , dataGridCell의 하위 컨트롤에 포커스를 전송하려면 여기를 클릭하십시오.
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="TextBoxPart" Property="FocusManager.FocusedElement" Value="{Binding ElementName=TextBoxPart}" />
</Trigger>
</ControlTemplate.Triggers>
이 템플릿에 추가 할 수 있습니다. 초점을 잘 옮겨야합니다. 접근성에 대한 같은
, 난이 도움이 될 것입니다 생각하지 않습니다,하지만 난 당신이 원하는 것을 달성 어떤 식 으로든 표시되지 않습니다 : -/
내가 장식 패턴을 사용하는 것이 사용할 수 있습니다, 등 XAML에서 컨트롤을 '감쌀'수 있습니다 (즉, 사용자 지정 컨트롤을 만들 필요가 없습니다). 나는 일을 마치고 오늘 예를 올릴 것이다. – Dennis