为什么mi ListBox没有恢复焦点?

时间:2014-09-12 12:53:26

标签: c# .net wpf xaml textbox

我的TextBox下面有一个ListBox,它充当了自动完成的TextBox。为了更好的解释,如果你看一下this视频会很棒。

这是我的代码:

xaml:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="200*" />
    </Grid.RowDefinitions>
    <TextBox 
        Name="TextAuto"
        Height="23" TextWrapping="NoWrap" Text="TextBox" VerticalAlignment="Top"
        PreviewKeyDown="TextAuto_OnPreviewKeyDown" />

    <ListBox Name="ListBoxSuggestion"
             Grid.Row="1" VerticalAlignment="Top"  Visibility="Collapsed" 
             PreviewKeyDown="ListBoxSuggestion_OnPreviewKeyDown"/>
</Grid>

背后的代码:

public MainWindow()
{
    InitializeComponent();
    nameList = new List<string>
    {
        "A0-Word", "B0-Word", "C0-Word",
        "A1-Word", "B1-Word", "C1-Word",
        "A2-Word", "B2-Word", "C2-Word",
        "A3-Word", "B3-Word", "C3-Word"
    };
    TextAuto.TextChanged += TextAuto_TextChanged;
}

void TextAuto_TextChanged(object sender, TextChangedEventArgs e)
{
    string typedString = TextAuto.Text;
    List<string> autoList = new List<string>();
    autoList.Clear();

    autoList.AddRange(nameList.Where(item => !string.IsNullOrEmpty(TextAuto.Text)).Where(item => item.StartsWith(typedString)));

    if (autoList.Count > 0)
    {
        ListBoxSuggestion.ItemsSource = autoList;
        ListBoxSuggestion.Visibility = Visibility.Visible;
    }
    else 
    {
        ListBoxSuggestion.Visibility = Visibility.Collapsed;
        ListBoxSuggestion.ItemsSource = null;
    }
}

private void TextAuto_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    if (!e.IsDown || e.Key != Key.Down) return;

    FocusManager.SetFocusedElement(this, ListBoxSuggestion);
    ListBoxSuggestion.SelectedIndex = 0;

}

private void ListBoxSuggestion_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.IsDown && e.Key == Key.Enter)
    {
        if (ListBoxSuggestion.ItemsSource != null)
        {
            ListBoxSuggestion.Visibility = Visibility.Collapsed;
            TextAuto.TextChanged -= TextAuto_TextChanged;
            if (ListBoxSuggestion.SelectedIndex != -1)
            {
                TextAuto.Text = ListBoxSuggestion.SelectedItem.ToString();
            }
            TextAuto.TextChanged += TextAuto_TextChanged;
        }
    }

    if (!e.IsDown || e.Key != Key.Up) return;
    if (ListBoxSuggestion.SelectedIndex != 0) return;

    FocusManager.SetFocusedElement(this, TextAuto);
    ListBoxSuggestion.SelectedIndex = -1;
}

TextBox时,如果按下,则会访问ListBox。在ListBox,当上升时,如果是SelectedIndex==0,我会将焦点重新放回TextBox。但是第二次我想把重点放回ListBox(第二次在TextBox内按向下键),ListBox看起来是灰色的,我无法访问它...... :(

但是,正如视频中所理解的那样,似乎ListBox永远无法恢复焦点!但在查看FocusManager.FocusedElement时,它表示 ListBox 具有焦点。 Keyboard.Focus也是如此。

可能会发生什么?

1 个答案:

答案 0 :(得分:1)

IsTabStop="False"添加到TextBox

<TextBox 
    Name="TextAuto"
    Height="23" TextWrapping="NoWrap" Text="TextBox" VerticalAlignment="Top"
    PreviewKeyDown="TextAuto_OnPreviewKeyDown" IsTabStop="False" />

由于一些奇怪的原因,它基本上得到了TabFocused。因为向下键也用于选择页面上的对象。