无法使 WPF 从 UserControl 的子控件传递焦点

发布于 2024-12-20 11:00:40 字数 4202 浏览 1 评论 0原文

我定义了一个名为 TouchTextBoxUserControl。我已经让 TouchTextBox 在接收焦点时将焦点提供给 TextBox。现在,当用户单击另一个控件或按下 Tab 时,我需要 TouchTextBox 放弃焦点。以下是 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 的子控件,然后在我的 MainWindow 中使用。当此控件获得焦点时,它会按设计显示弹出窗口。但不知何故,当我单击另一个控件或按选项卡时,它并没有放弃焦点。

我必须改变什么才能使这项工作正常进行?

托尼

I've defined a UserControl called TouchTextBox. I've gotten the TouchTextBox to give focus to the TextBox when it receives focus. Now I need the TouchTextBox to give up focus when the user clicks in another control or when they press tab. Here's some of the XAML for the 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>

This control is used as a child control on a couple of UserControl's that are then used in my MainWindow. When this control receives focus, it displays the Popup as designed. But somehow it's not giving up the focus when I click in another control or press tab.

What do I have to change to make this work?

Tony

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2024-12-27 11:00:40

我通常使用的解决方法是在我的 UserControl 上设置 IsTabStop="False" (以防止按 Tab 键切换到 UserControl 本身),然后在 UserControl 内部使用 TemplateBinding 将内部控件的 TabIndex 绑定到 UserControlTabIndex

请参阅此问题:在 wpf 中设置 Tab 键顺序

The workaround I usually use is to set the IsTabStop="False" on my UserControl (to prevent tabbing onto the UserControl itself), and then inside the UserControl use a TemplateBinding to bind to the inner control's TabIndex to the UserControl's TabIndex.

See this question: Setting tab order in wpf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文