WPF 可搜索组合框

LoP*_*PeZ -1 wpf combobox focus items

我有ComboBox如下:

<ComboBox IsEditable="True" 
          Width="200" 
          Height="25" 
          IsTextSearchEnabled="False" 
          x:Name="cb" 
          PreviewTextInput="Cb_OnPreviewTextInput" 
          ItemsSource="{Binding ItemList}" 
          Text="{Binding SearchTextText}">
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

什么时候Cb_OnPreviewTextInput叫我定IsDropdownOpen = true。在第一次尝试中(输入第一个字母后),选择了列表中的第一项,我可以使用相关箭头上下移动,插入符号仍然在TextBox.

当我继续打字时,我无法再上下导航(一次 1 项);此时,整个 ScrollViewer 获得焦点,我只能转到底部或顶部,但不能一一对应。我必须关闭弹出窗口,例如按 Escape 键,然后通过键入 1 个字符重新打开才能向上移动/再次下降。

我还注意到,按 PageUp 后,第一个项目也会被选中,所以我尝试在代码中模仿,但没有成功。

有谁知道在这里该怎么做才能顺利向上/向下导航和打字?

Zoz*_*ozo 6

当我尝试在隔离环境中重现您的问题时,一切似乎都很好......

在此输入图像描述

您使用的是哪个 .NET 版本?

我使用过这样的代码:

<Window x:Class="InterviewApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="350"
        Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <WrapPanel Orientation="Horizontal">
        <ComboBox IsEditable="True"
                  Width="200"
                  Height="25"
                  IsTextSearchEnabled="False"
                  x:Name="cb"
                  PreviewTextInput="Cb_OnPreviewTextInput"
                  ItemsSource="{Binding ItemList}"
                  Text="{Binding SearchTextText}">
            <ComboBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel />
                </ItemsPanelTemplate>
            </ComboBox.ItemsPanel>
        </ComboBox>
    </WrapPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

后面的代码看起来像这样

namespace Application
{
    public partial class MainWindow : INotifyPropertyChanged
    {
        public MainWindow()
        {
            ItemList = new ObservableCollection<string>();
            for (var i = 0; i < 1000; i++)
            {
                ItemList.Add($"Item {i}");
            }

            InitializeComponent();
        }

        private void Cb_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            cb.IsDropDownOpen = true;
        }

        public ObservableCollection<string> ItemList { get; set; }

        public string SearchTextText
        {
            get => _searchTextText;
            set
            {
                if (_searchTextText == value) return;
                _searchTextText = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SearchTextText)));
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}
Run Code Online (Sandbox Code Playgroud)

  • 它没有按照我所描述的那样操作 (2认同)