写点什么

WPF 如何自定义隐藏下拉框选项

作者:DisonTangor
  • 2022 年 8 月 01 日
  • 本文字数:1810 字

    阅读完需:约 6 分钟

目前很多的 Client 都是基于 JS 的,但是总有时候会遇到其他的框架不如 Qt、WPF 和 JavaFx 等。那么这次我就讲解一个 WPF 的技巧:如何自定义隐藏下拉框选项


demo.xaml


<ComboBox  IsTextSearchEnabled="False"                        IsEditable="True"                      ItemsSource="source" <!--source {value:string, text:string }-->                      KeyUp="comboBox_KeyUp" DropDownOpened="OnDropDownOpened"   DropDownClosed="OnDropDownClosed"/>
复制代码


demo.xaml.cs


/// <summary>/// 搜索条件过滤/// </summary> private void comboBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) {         var combobox = (ComboBox)sender;        combobox.SelectedItem = null;        var ctb = combobox.Template.FindName("PART_EditableTextBox", combobox) as TextBox;        if (ctb == null) return;        if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift) || Keyboard.Modifiers.HasFlag(ModifierKeys.Control) || Keyboard.Modifiers.HasFlag(ModifierKeys.Alt))            return;        var caretPos = ctb.CaretIndex;        combobox.IsDropDownOpen = true;
var datasource = (combobox.ItemsSource as List<SelectOption>).DeepClone().Where(x => /*自定义条件*/)).ToList(); combobox.ItemsSource = datasource;
CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultView(combobox.Items); itemsViewOriginal.Filter = ((o) => { if (String.IsNullOrEmpty(combobox.Text)) { return true; } else { if (((SelectOption)o).text.ToLower().Contains(combobox.Text.ToLower()))//(SelectOption)o).text.StartsWith(combobox.Text, true, null) { return true; } else { return false; } } });
itemsViewOriginal.Refresh(); ctb.CaretIndex = caretPos; } /// <summary> /// 下拉框初始化过滤 /// </summary> private void OnDropDownOpened(object sender, EventArgs e) { var cb = (ComboBox)sender; if (cb.IsDropDownOpen == true) { var combobox = (ComboBox)sender; //combobox.SelectedItem = null; var ctb = combobox.Template.FindName("PART_EditableTextBox", combobox) as TextBox; if (ctb == null) return; if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift) || Keyboard.Modifiers.HasFlag(ModifierKeys.Control) || Keyboard.Modifiers.HasFlag(ModifierKeys.Alt)) return; var caretPos = ctb.CaretIndex; combobox.IsDropDownOpen = true;
var tempValue = combobox.SelectedValue;
var datasource = (combobox.ItemsSource as List<SelectOption>).DeepClone() .Where(x => /*自定义条件*/) .ToList(); combobox.ItemsSource = datasource;
CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultView(combobox.Items);
itemsViewOriginal.Refresh(); combobox.SelectedValue = tempValue; ctb.CaretIndex = caretPos; } } /// <summary> /// 下拉框数据源还原 /// </summary> private void OnDropDownClosed(object sender, EventArgs e) { var cb = (ComboBox)sender; if (cb.IsDropDownOpen == false) { var tempValue = cb.SelectedValue; cb.ItemsSource = (this.DataContext as /*定义的前端ViewModel*/).source; cb.SelectedValue = tempValue;
} }
复制代码

结语

原理看代码就会了,具体如何自定义过滤,结合自己的实际需求设计。

发布于: 刚刚阅读数: 4
用户头像

DisonTangor

关注

怀揣一个武侠梦的男孩 2020.07.29 加入

还未添加个人简介

评论

发布
暂无评论
WPF如何自定义隐藏下拉框选项_WPF_DisonTangor_InfoQ写作社区