Can';t使WPF从UserControl的子控件传递焦点
我定义了一个名为Can';t使WPF从UserControl的子控件传递焦点,wpf,user-controls,focus,focusmanager,Wpf,User Controls,Focus,Focusmanager,我定义了一个名为TouchTextBox的UserControl。我已经让触摸文本框在它接收焦点时为文本框提供焦点。现在我需要TouchTextBox在用户单击另一个控件或按tab键时放弃焦点。下面是一些用于TouchTextBox的XAML <UserControl x:Class="CarSystem.CustomControls.TouchTextBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml
TouchTextBox
的UserControl
。我已经让触摸文本框
在它接收焦点时为文本框
提供焦点。现在我需要TouchTextBox
在用户单击另一个控件或按tab键时放弃焦点。下面是一些用于TouchTextBox
的XAML
<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>
此控件用作两个UserControl
上的子控件,然后在我的main窗口中使用这些控件。当此控件接收到焦点时,它会按设计显示弹出窗口。但不知何故,当我点击另一个控件或按tab键时,它并没有放弃焦点
我需要做什么改变才能让这一切顺利进行
Tony我通常使用的解决方法是在我的UserControl
上设置IsTabStop=“False”
(以防止切换到UserControl
本身),然后在UserControl
内部使用TemplateBinding
将内部控件的TabIndex
绑定到UserControl
的TabIndex
请参阅此问题:请向我们展示事件处理程序(LostFocus、GotFoucs)中的代码。这是否回答了您的问题?这是一个骗局。。。被接受的答案只是重复那里的答案。