4

ListBoxとボタンを含むWPFポップアップコントロールがあります。をクリックするButtonと、無効になるはずです。問題は、ボタンを無効にすると、ポップアップからTabキーが消えることです。ButtonIsEnabledをfalseに設定した後、フォーカスをListBoxに設定しようとしましたが、機能しませんでした。では、ポップアップコントロール内のリストボックスにタブフォーカスを設定するにはどうすればよいですか?

これが私のコードです。

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

そしてここにスクリーンショットがあります:

代替テキストhttp://img11.imageshack.us/img11/6305/popuptabkey.png

後で編集:

回避策を見つけました。それは、次のようにFocusListBox();通話を遅らせることです。

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

1 に答える 1

4

ポップアップでFocusManager.IsFocusScopeプロパティを設定して、明示的なフォーカススコープを定義する必要があります。

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

これにより、フォーカスが含まれている要素内のコントロールに戻るのを防ぎます。

于 2009-10-19T23:57:53.423 に答える