Не удается передать фокус WPF из дочернего элемента управления UserControl

Я определил UserControl с именем TouchTextBox. Я получил TouchTextBox, чтобы передать фокус TextBox, когда он получает фокус. Теперь мне нужно TouchTextBox, чтобы отказаться от фокуса, когда пользователь щелкает другой элемент управления или когда он нажимает вкладку. Вот часть XAML для TouchTextBox.

<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, которые затем используются в моем MainWindow. Когда этот элемент управления получает фокус, он отображает всплывающее окно, как задумано. Но почему-то он не теряет фокус, когда я нажимаю другой элемент управления или нажимаю вкладку.

Что мне нужно изменить, чтобы это заработало?

Тони


person Tony Vitabile    schedule 10.12.2011    source источник
comment
Покажите, пожалуйста, код в обработчиках событий (LostFocus, GotFoucs).   -  person FunnyItWorkedLastTime    schedule 10.12.2011
comment
Отвечает ли это на ваш вопрос? Установка порядка табуляции в wpf при использовании из UserControls?   -  person StayOnTarget    schedule 21.05.2021
comment
Это обман stackoverflow.com/questions/7954973/ ... принятый ответ просто повторяет там ответ.   -  person StayOnTarget    schedule 21.05.2021


Ответы (1)


Обходной путь, который я обычно использую, состоит в том, чтобы установить IsTabStop="False" на моем UserControl (чтобы предотвратить табуляцию на самом UserControl), а затем внутри UserControl использовать TemplateBinding для привязки TabIndex внутреннего элемента управления к TabIndex UserControl.

См. этот вопрос: Настройка порядка табуляции в wpf

person M.Azad    schedule 10.12.2011