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

时间:2019-10-09 10:39:28

标签: c# wpf xaml

我在画布上有两个按钮(Focusable = true) 我使用Tab键选择一个按钮,然后按Enter键将其删除 我想让(键盘)焦点回到画布上,但是转到窗口上

演示: 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:

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);
        }
    }
}

应用程序启动时,所有内容均为红色。 按下TAB键后,画布变为绿色 再次按TAB键,按钮1变为绿色,画布变为红色 按ENTER键,按钮1消失,但画布未变绿 我希望画布变绿

2 个答案:

答案 0 :(得分:1)

您可以将焦点手动移至“画布”:

//

在按钮回调中:

cubeN.obj

更新: 您可以使用隐藏代码中的# This cube has a different material # applied to each of its faces. mtllib master.mtl v 0.000000 2.000000 2.000000 v 0.000000 0.000000 2.000000 v 2.000000 0.000000 2.000000 v 2.000000 2.000000 2.000000 v 0.000000 2.000000 0.000000 v 0.000000 0.000000 0.000000 v 2.000000 0.000000 0.000000 v 2.000000 2.000000 0.000000 vn 0 0 1 vn 0 0 -1 vn 1 0 0 vn 0 1 0 vn -1 0 0 vn 0 -1 0 # 8 vertices g front usemtl red f 1//1 2//1 3//1 4//1 g back usemtl blue f 8//2 7//2 6//2 5//2 g right usemtl green f 4//3 3//3 7//3 8//3 g top usemtl gold f 5//4 1//4 4//4 8//4 g left usemtl orange f 5//5 6//5 2//5 1//5 g bottom usemtl purple f 2//6 6//6 7//6 3//6 # 6 elements 方法将焦点移至下一个元素:

Canvas.Focus();

同样,您可以将焦点从窗口移到画布上。

答案 1 :(得分:0)

我在XAML中更改了以下内容:

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

并在我的代码后面添加了该方法:

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

这似乎是在我按一下按钮时将(键盘)焦点移到“画布”上的方法。