WPF Popup tab key bug

本小妞迷上赌 提交于 2020-06-23 03:51:32

问题


I have a WPF Popup control with a ListBox and a Button in it. When I click the Button, it should become disabled. The problem is, that when I disable the Button, the Tab key goes out from the Popup. I tried to set the focus to the ListBox, after I set the Button's IsEnabled to false, but that did not work. So, how can I set the tab focus to the ListBox inside the Popup control ?

Here is my code.

Window1.xaml:

<Window x:Class="WpfApplication5.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <StackPanel>
        <Button Name="openButton" Content="Open"/>
        <Popup Name="popup" Placement="Center">
            <StackPanel>
                <ListBox Name="listBox"/>
                <Button Name="newItemsButton" Content="New Items"/>
            </StackPanel>
        </Popup>
    </StackPanel>
</Window>

Window1.xaml.cs:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApplication5
{
    partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            openButton.Focus();
            listBox.ItemsSource = new string[] { "Item1", "Item2", "Item3" };
            listBox.SelectedIndex = 1;

            openButton.Click += delegate { popup.IsOpen = true; };
            popup.Opened += delegate { FocusListBox(); };
            newItemsButton.Click += delegate
            {
                newItemsButton.IsEnabled = false;
                FocusListBox();
            };
        }

        void FocusListBox()
        {
            var i = listBox.ItemContainerGenerator.ContainerFromIndex(
                listBox.SelectedIndex) as ListBoxItem;
            if (i != null)
                Keyboard.Focus(i);
        }
    }
}

And here's a screenshot:

alt text http://img11.imageshack.us/img11/6305/popuptabkey.png

Later Edit:

I've found a workaround, that is to delay the FocusListBox(); call as below:

Dispatcher.BeginInvoke(new Action(FocusListBox), DispatcherPriority.Input);

回答1:


You need to define an explicit focus scope by setting the FocusManager.IsFocusScope property on the Popup:

<Popup FocusManager.IsFocusScope="true">
  <!-- your content here -->
</Popup>

This will keep the focus from moving back out to controls within the containing element.



来源:https://stackoverflow.com/questions/1591726/wpf-popup-tab-key-bug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!