Strange focus behavior for simple WPF ItemsControl items

I see strange behavior when it comes to focusing and navigating the keyboard. In the example below, I have a simple ItemsControl element that has been configured to represent a list of CheckBox bound to ItemsSource.

<ItemsControl FocusManager.IsFocusScope="True"
              ItemsSource="{Binding ElementName=TheWindow, Path=ListOStrings}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

For some strange reason, setting FocusManager.IsFocusScope = "True" means that the keyboard focus cannot be set when the checkbox is clicked and the focus exits the ItemsControl when the box is checked using the space bar on the keyboard. Both symptoms seem to indicate some kind of strange navigation that occurs when the checkbox is checked, but it’s not easy for me to get to it.

This problem occurs if I set any parent in the visual tree as the focus area using this method. If I remove FocusManager.IsFocusScope = "True", then the problems go away. Unfortunately, I see this problem in a larger project where I cannot just remove these focus areas without worrying about other focus-related effects.

Can someone explain to me the strange behavior that I see? Is this a mistake or am I just missing something?

+5
source share
2 answers

This article is very well explained: http://www.codeproject.com/KB/WPF/EnhancedFocusScope.aspx

What was developed by FocusScope?

Microsoft FocusScope WPF . WPF .

:

, , . , "" . , .

? - ? , ( WPF); .

,

, CheckBox WPF , ?

, , : . ButtonBase .

+16

@Meleak . http://www.codeproject.com/KB/WPF/EnhancedFocusScope.aspx, , . IsEnhancedFocusScope, :

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

XAML IsFocusScope:

<ItemsControl my:FocusExtensions.IsEnhancedFocusScope="True"
              ItemsSource="{Binding ElementName=TheWindow, Path=ListOStrings}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

, , .

+8

All Articles