【问题标题】:Set (keyboard)focus to parent container when child with (keyboard)focus is removed当具有(键盘)焦点的子元素被移除时,将(键盘)焦点设置为父容器
【发布时间】:2019-10-09 19:22:34
【问题描述】:

我在画布中有两个按钮(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,Button 1 变为绿色,canvas 变为红色 按 ENTER,按钮 1 消失,但画布没有变成绿色 我想让画布变成绿色

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    您可以手动将焦点移至 Canvas:

    Canvas.Focus();
    

    在您的按钮回调中:

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

    更新: 您可以使用代码隐藏中的UIElement.MoveFocus 方法将焦点移至下一个元素:

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

    您也可以通过这种方式将焦点从窗口移动到画布。

    【讨论】:

    • 我想找到一种更自动化的方法来实现它,想象一下在画布中有许多不同的元素,它们不会使用代码隐藏来删除自己。
    • 感谢您的更新,我的实际用例中实际上并没有“RemoveSelf”功能,因此无法用于更改焦点。似乎这会将焦点移到画布中的下一个元素,但我需要将焦点放在画布上。
    【解决方案2】:

    我在 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();
        }
    }
    

    这似乎将(键盘)焦点移动到画布上,只要我按下按钮上的 ENTER,这是我需要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-08
      • 2016-01-06
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多