已筛选组合框上的箭头键
本文关键字:筛选 组合 | 更新日期: 2023-09-27 18:29:11
我有一个简单的组合框,其中有一个使用.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");
}
当用户键入时,一切都很好,但一旦使用箭头键进行选择,过滤后的列表就会消失,组合框中的值也会被清除。如何让用户使用箭头键浏览用户最初键入时显示的筛选列表?
我怀疑它是您的"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");
{
}
在考虑了@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");
}
}
真不敢相信事情竟然这么简单。感谢所有的投入!