简体   繁体   English

当具有(键盘)焦点的子项被移除时,将(键盘)焦点设置为父容器

[英]Set (keyboard)focus to parent container when child with (keyboard)focus is removed

I have two buttons in a canvas (with Focusable=true) I use the tab key to select one button and press enter to remove the button I want (keyboard)focus to go back to canvas, but it goes to window I have two buttons in a canvas (with Focusable=true) I use the tab key to select one button and press enter to remove the button I want (keyboard)focus to go back to canvas, but it goes to window

Demo: MainWindow.xaml演示:MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <Style TargetType="Button">
                <Setter Property="Background" Value="Red" />
                <Style.Triggers>
                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="Background" Value="Green" />
                    </Trigger>
                </Style.Triggers>
            </Style>
            <Style TargetType="Canvas">
                <Setter Property="Background" Value="Red" />
                <Style.Triggers>
                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="Background" Value="Green" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>
        <Canvas Name="Canvas" Focusable="True">
            <Button Click="RemoveSelf">Button 1</Button>
            <Button Canvas.Left="100" Click="RemoveSelf">Button 2</Button>
        </Canvas>
    </Grid>
</Window>

MainWindow.xaml.cs: MainWindow.xaml.cs:

using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void RemoveSelf(object sender, RoutedEventArgs e)
        {
            Canvas.Children.Remove((UIElement)sender);
        }
    }
}

When the application starts, everything is red.当应用程序启动时,一切都是红色的。 As soon as you press TAB, canvas becomes green Press TAB again, and Button 1 becomes green, canvas becomes red Press ENTER, button 1 disappears but canvas does not become green I want canvas to become green As soon as you press TAB, canvas becomes green Press TAB again, and Button 1 becomes green, canvas becomes red Press ENTER, button 1 disappears but canvas does not become green I want canvas to become green

You can move focus to Canvas manually:您可以手动将焦点移至 Canvas:

Canvas.Focus();

In your button callback:在您的按钮回调中:

private void RemoveSelf(object sender, RoutedEventArgs e)
{
    Canvas.Children.Remove((UIElement)sender);
    Canvas.Focus();
}

UPDATE: You can move focus to the next element using UIElement.MoveFocus method in code-behind:更新:您可以在代码隐藏中使用UIElement.MoveFocus方法将焦点移动到下一个元素:

private void RemoveSelf(object sender, RoutedEventArgs e)
{
    if (sender is UIElement element)
    {
        element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        // ...
    }
}

Also this way you can move focus from window to canvas.同样,您可以将焦点从 window 移动到 canvas。

I changed the following in my XAML:我在 XAML 中更改了以下内容:

<Canvas Name="Canvas" Focusable="True" IsKeyboardFocusWithinChanged="FocusWithinChanged">

And added the method in my codebehind:并在我的代码隐藏中添加了该方法:

private void FocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    var flag = e.NewValue as bool?;
    if (flag != true)
    {
        Canvas.Focus();
    }
}

This appears to move the (keyboard)focus to the Canvas as soon as I press ENTER on a button, which is what I need.这似乎将(键盘)焦点移动到 Canvas 一旦我按下按钮上的 ENTER ,这是我需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM