【问题标题】:WPF searchable comboboxWPF 可搜索组合框
【发布时间】:2017-06-26 16:33:29
【问题描述】:

我有一个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>

Cb_OnPreviewTextInput 被调用时,我设置IsDropdownOpen = true。 在第一次尝试中(在输入第一个字母后)列表中的第一个项目被选中,我可以使用相关箭头上下移动,插入符号仍在 TextBox 中。

当我继续打字时,我不能再上下导航(一次 1 项),此时整个 ScrollViewer 获得焦点,我只能转到底部或顶部,但不是 1 比 1。我必须关闭弹出窗口,例如按 Escape,然后键入 1 个字符重新打开,以便能够再次向上/向下。

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

任何人都知道如何在此处进行向上/向下导航和输入而不会出现问题?

【问题讨论】:

  • 请修正您的代码(关闭所有标签)和说明内的拼写。另外,请粘贴在 Cb_OnPreviewTextInput 正文中。我假设是 ItemList 的字符串集合吗?

标签: wpf combobox focus items


【解决方案1】:

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

您使用的是哪个 .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>

后面的代码是这样的

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

【讨论】:

  • 它不符合我的描述
【解决方案2】:

Jordi 在此处创建过滤组合框的最佳答案:Dynamic filter of WPF combobox based on text input 如果你这样做,你可以在 xaml 中这样实现:

&lt;local:FilterableComboBox Width="250" Height="25" ItemsSource="{Binding ItemList}" SelectedItem="{Binding SelectedItem}" OnlyValuesInList="True"/&gt;

【讨论】:

    猜你喜欢
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2013-03-30
    • 2013-11-08
    • 1970-01-01
    相关资源
    最近更新 更多