【问题标题】:Arrow keys on filtered combo box过滤组合框上的箭头键
【发布时间】:2016-04-09 10:44:09
【问题描述】:

我有一个简单的组合框,它有一个使用 .DefaultView 作为项目源的数据表。我在组合框上放置了一个过滤器,代码如下:

    private void FilterCombobox(ComboBox cmb, string columnName)
    {
        DataView view = (DataView)cmb.ItemsSource;
        view.RowFilter = (columnName + " like '*" + cmb.Text + "*'");

        cmb.ItemsSource = view;
        cmb.IsDropDownOpen = true;
    }

组合框的 XAML 是:

<ComboBox x:Name="cmbRigNum" KeyboardNavigation.TabIndex="3" HorizontalAlignment="Left" Margin="470,440,0,0" VerticalAlignment="Top" Width="206" SelectionChanged="cmbRigNum_SelectionChanged" IsEditable="True" StaysOpenOnEdit="True" IsTextSearchEnabled="False" FontFamily="Arial" FontSize="14" KeyUp="cmbRigNum_KeyUp"/>     

更新:Key_Up 事件:

    private void cmbRigNum_KeyUp(object sender, KeyEventArgs e)
    {
        FilterCombobox(cmbRigNum, "RigNumber");         
    }

当用户键入时,一切都很好,但是一旦使用箭头键进行选择,过滤的列表就会消失,组合框中的值也会被清除。如何使用户能够使用箭头键在用户最初键入时显示的过滤列表中导航?

【问题讨论】:

    标签: c# wpf filter combobox


    【解决方案1】:

    我怀疑这是您的“cmbRigNum_KeyUp”方法中的内容。

    编辑: 因此,如果您不希望它使用箭头键更改过滤器,您可以这样做吗?

    private void cmbRigNum_KeyUp(object sender, KeyEventArgs e)
    {
        if (char.IsLetter(e.Keychar) || char.IsDigit(e.KeyChar)) // Add more characters as needed.
        {
            FilterCombobox(cmbRigNum, "RigNumber");
        {
    }
    

    【讨论】:

    • @BabyDoll ...见编辑。如果你想要除箭头键之外的所有键,那么你可以寻找它们不是 Keychar。
    • 我只需要用户使用箭头进行选择(浏览项目)。当前,当过滤器列表出现并按下向下箭头(例如)时,什么也没有发生。所以当我按下箭头时它不会改变过滤器,但它也不会开始导航
    • WPF 中没有 KeyChar 属性,但我使用 e.Key 实现了类似的东西。但仍然没有运气。就像按下箭头时它没有注册它需要选择一个项目/索引
    【解决方案2】:

    在尝试了@Topher Birth 给出的建议后,我找到了解决问题的方法。对于遇到同样问题的人来说,这里是解决问题的代码:

         private void FilterCombobox(ComboBox cmb, string columnName)
        {
            //because the itemsSource of the comboboxes are datatables, filtering is not supported. Converting the itemsSource to a
            //dataview will allow the functionality of filtering to be implemented
            DataView view = (DataView)cmb.ItemsSource;
            view.RowFilter = (columnName + " like '*" + cmb.Text + "*'");
    
            cmb.ItemsSource = view;
            cmb.IsDropDownOpen = true;
        }
    
        private void cmbRigNum_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Down & e.Key != Key.Up) 
            {
                e.Handled = true;
                FilterCombobox(cmbRigNum, "RigNumber");
            }
        }
    

    不敢相信它竟然这么简单。感谢您的所有意见!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-05
      • 2012-12-03
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多