0

这是一些重现问题的示例代码:

<StackPanel Width="100">
    <ToggleButton Content="Test 1"/>
    <ToggleButton Content="Test 2"/>
    <ToggleButton Content="Test 3"/>
    <StackPanel FocusManager.IsFocusScope="True">
        <ToggleButton Content="Test 4"/>
        <ToggleButton Content="Test 5"/>
        <ToggleButton Content="Test 6"/>
    </StackPanel>
</StackPanel>

如果您通过切换按钮切换,前 3 个您可以使用空格键选中/取消选中没有问题,因为键盘焦点仍然在您更改的项目上。但是,如果您单击/切换焦点范围内的框 4-6 并尝试通过空格键更改它们,焦点将在该焦点范围之外重置,并且随后的空格键按下将在焦点范围之外执行。

在 where 更改数据时如何防止键盘焦点离开IsFocusScope=True

4

1 回答 1

0

我发现这种行为是 WPF 的“设计使然”。这篇文章最好地解释了它:https ://stackoverflow.com/a/4954794/302677

创建附加属性以实现您自己的焦点范围处理的建议解决方案也在该链接中。

public static class FocusExtensions
{
    private static bool SettingKeyboardFocus { get; set; }

    public static bool GetIsEnhancedFocusScope(DependencyObject element) {
        return (bool)element.GetValue(IsEnhancedFocusScopeProperty);
    }

    public static void SetIsEnhancedFocusScope(DependencyObject element, bool value) {
        element.SetValue(IsEnhancedFocusScopeProperty, value);
    }

    public static readonly DependencyProperty IsEnhancedFocusScopeProperty =
        DependencyProperty.RegisterAttached(
            "IsEnhancedFocusScope",
            typeof(bool),
            typeof(FocusExtensions),
            new UIPropertyMetadata(false, OnIsEnhancedFocusScopeChanged));

    private static void OnIsEnhancedFocusScopeChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e) {
        var item = depObj as UIElement;
        if (item == null)
            return;

        if ((bool)e.NewValue) {
            FocusManager.SetIsFocusScope(item, true);
            item.GotKeyboardFocus += OnGotKeyboardFocus;
        }
        else {
            FocusManager.SetIsFocusScope(item, false);
            item.GotKeyboardFocus -= OnGotKeyboardFocus;
        }
    }

    private static void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) {
        if (SettingKeyboardFocus) {
            return;
        }

        var focusedElement = e.NewFocus as Visual;

        for (var d = focusedElement; d != null; d = VisualTreeHelper.GetParent(d) as Visual) {
            if (FocusManager.GetIsFocusScope(d)) {
                SettingKeyboardFocus = true;

                try {
                    d.SetValue(FocusManager.FocusedElementProperty, focusedElement);
                }
                finally {
                    SettingKeyboardFocus = false;
                }

                if (!(bool)d.GetValue(IsEnhancedFocusScopeProperty)) {
                    break;
                }
            }
        }
    }
}
于 2021-09-30T17:42:32.370 回答