Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么mi ListBox没有恢复焦点?_C#_.net_Wpf_Xaml_Textbox - Fatal编程技术网

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

C# 为什么mi ListBox没有恢复焦点?,c#,.net,wpf,xaml,textbox,C#,.net,Wpf,Xaml,Textbox,我有一个文本框,下面有一个列表框,充当自动完成文本框。为了得到更好的解释,如果你看一看视频就好了 这是我的代码: xaml: <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="200*" /> </Grid.RowDefinitions> <TextBox

我有一个
文本框
,下面有一个
列表框
,充当自动完成文本框。为了得到更好的解释,如果你看一看视频就好了

这是我的代码:

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>

隐藏的代码:

<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主窗口()
{
初始化组件();
名称列表=新列表
{
“A0字”、“B0字”、“C0字”,
“A1字”、“B1字”、“C1字”,
“A2字”、“B2字”、“C2字”,
“A3单词”、“B3单词”、“C3单词”
};
TextAuto.TextChanged+=TextAuto\u TextChanged;
}
void TextAuto_TextChanged(对象发送者,TextChangedEventArgs e)
{
字符串类型字符串=TextAuto.Text;
List autoList=新列表();
autoList.Clear();
autoList.AddRange(nameList.Where(item=>!string.IsNullOrEmpty(TextAuto.Text)).Where(item=>item.StartsWith(typedString));
如果(autoList.Count>0)
{
ListBoxSuggestion.ItemsSource=自动列表;
ListBoxSuggestion.Visibility=可见性.Visibility;
}
其他的
{
ListBoxSuggestion.Visibility=可见性。已折叠;
ListBoxSuggestion.ItemsSource=null;
}
}
私有void TextAuto_OnPreviewKeyDown(对象发送者,KeyEventArgs e)
{
如果(!e.IsDown | e.Key!=Key.Down)返回;
SetFocusedElement(这个,ListBoxSuggestion);
ListBoxSuggestion.SelectedIndex=0;
}
private void ListBoxSuggestion_OnPreviewKeyDown(对象发送者,KeyEventArgs e)
{
如果(e.IsDown&&e.Key==Key.Enter)
{
if(ListBoxSuggestion.ItemsSource!=null)
{
ListBoxSuggestion.Visibility=可见性。已折叠;
TextAuto.TextChanged-=TextAuto\u TextChanged;
如果(ListBoxSuggestion.SelectedIndex!=-1)
{
TextAuto.Text=ListBoxSuggestion.SelectedItem.ToString();
}
TextAuto.TextChanged+=TextAuto\u TextChanged;
}
}
如果(!e.IsDown | e.Key!=Key.Up)返回;
如果(ListBoxSuggestion.SelectedIndex!=0)返回;
SetFocusedElement(这个,TextAuto);
ListBoxSuggestion.SelectedIndex=-1;
}
文本框中时
,如果按下,则访问
列表框
。在
列表框
中,当向上移动时,如果
选择的索引==0
,我将焦点返回到
文本框
。但是第二次我想把焦点放回
列表框
(第二次我在
文本框
内按下向下键),列表框看起来是灰色的,我无法访问它……:(

但是,正如在视频中所看到的,列表框似乎永远无法恢复焦点!!但是当查看
FocusManager.FocusedElement
时,它会说列表框有焦点。
键盘.focus
也是如此

会发生什么?

在文本框中添加一个
IsTabStop=“False”

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

它基本上是因为一些奇怪的原因而变得聚焦。因为向下键也用于选择页面上的对象