TouchTextBox
이라는 UserControl
을 정의했습니다. 포커스를 받으면 TouchTextBox
에 TextBox
에 초점을 둡니다. 이제 사용자가 다른 컨트롤을 클릭하거나 탭을 누를 때 포커스를 포기하기 위해 TouchTextBox
이 필요합니다. 다음은 TouchTextBox
의 XAML 중 일부입니다.WPF에서 UserControl의 자식 컨트롤에서 포커스를 전달할 수 없습니다.
<UserControl x:Class="CarSystem.CustomControls.TouchTextBox"
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"
xmlns:fps="clr-namespace:FPS.VirtualKeyboard;assembly=FPS.VirtualKeyboard"
mc:Ignorable="d"
Focusable="True"
GotFocus="UserControl_GotFocus">
<Grid Margin="0">
<Border BorderBrush="{Binding Path=BorderBrush, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
BorderThickness="{Binding Path=BorderThickness, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
FocusManager.IsFocusScope="True"
VerticalAlignment="{Binding Path=VerticalAlignment, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
<TextBox AcceptsTab="False"
AcceptsReturn="False"
Background="White"
Cursor="{Binding Path=Cursor, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
FlowDirection="{Binding Path=FlowDirection, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
FontFamily="{Binding Path=FontFamily, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
FontSize="{Binding Path=FontSize, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
FontStretch="{Binding Path=FontStretch, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
FontStyle="{Binding Path=FontStyle, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
FontWeight="{Binding Path=FontWeight, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
GotFocus="TextBox_GotFocus"
HorizontalAlignment="{Binding Path=HorizontalAlignment, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
HorizontalContentAlignment="{Binding Path=HorizontalContentAlignment, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
IsEnabled="{Binding Path=IsEnabled, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
LostFocus="TextBox_LostFocus"
Margin="1"
MaxLength="{Binding Path=MaxLength, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
MaxLines="{Binding Path=MaxLines, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
Name="TextBox"
Text="{Binding Path=Text, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
TextChanged="TextBox_TextChanged" />
</Border>
<Popup IsOpen="{Binding Path=PopupIsOpen, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
Name="KeyboardPopup"
PlacementTarget="{Binding Path=PlacementTarget, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
<fps:VirtualKeyboard AutoFill="True" Name="PopupKeyboard" />
</Popup>
</Grid>
</UserControl>
이 컨트롤은 내 MainWindow
에 사용되는 UserControl
's의 부부 자식 컨트롤로 사용됩니다. 이 컨트롤이 포커스를 받으면 설계된대로 팝업을 표시합니다. 하지만 어떻게 든 다른 컨트롤이나 탭을 클릭하면 포커스를 포기하지 않습니다.
이 작업을하려면 무엇을 변경해야합니까?
토니
이 우리에게 이벤트 핸들러 (LostFocus, GotFoucs)의 코드를 제시해주십시오. –