第
WPF实现筛选下拉多选控件的示例代码
VisualStudio2025;
接着上一篇,MultiSelectionSearchComboBox无法做到根据值选中选项,使用Selecteditems时会默认选中集合中的最后一个切选项选择功能会失效,这明显是不对的issue.
因为先前的OnSelectedItemsChanged逻辑处理错误,导致Binding多个选择值只保留了最后一次,因为之前为对listBox.SelectionChanged取消订阅。
实现代码
1)修改MultiSelectionSearchComboBox.cs代码如下:
using?System;
using?System.Collections;
using?System.Collections.Generic;
using?System.Linq;
using?System.Runtime.InteropServices;
using?System.Windows;
using?System.Windows.Controls;
using?System.Windows.Controls.Primitives;
using?System.Windows.Interop;
using?WPFDevelopers.Helpers;
namespace?WPFDevelopers.Controls
????[TemplatePart(Name?=?PART_TextBox,?Type?=?typeof(TextBox))]
????[TemplatePart(Name?=?PART_Popup,?Type?=?typeof(Popup))]
????[TemplatePart(Name?=?PART_Selector,?Type?=?typeof(ListBox))]
????[TemplatePart(Name?=?PART_SelectAll,?Type?=?typeof(CheckBox))]
????[TemplatePart(Name?=?PART_SearchSelector,?Type?=?typeof(ListBox))]
????public?class?MultiSelectionSearchComboBox?:?Control
????{
????????private?const?string?TextBoxTemplateName?=?PART_TextBox;
????????private?const?string?PopupTemplateName?=?PART_Popup;
????????private?const?string?ListBoxTemplateName?=?PART_Selector;
????????private?const?string?CheckBoxTemplateName?=?PART_SelectAll;
????????private?const?string?ListBoxTemplateNameSearch?=?PART_SearchSelector;
????????public?static?readonly?RoutedEvent?ClosedEvent?=
????????????EventManager.RegisterRoutedEvent(Closed,
????????????????RoutingStrategy.Bubble,
????????????????typeof(RoutedEventHandler),
????????????????typeof(MultiSelectionSearchComboBox));
????????public?static?readonly?DependencyProperty?DisplayMemberPathProperty?=
????????????DependencyProperty.Register(DisplayMemberPath,
????????????????typeof(string),
????????????????typeof(MultiSelectionSearchComboBox),
????????????????new?PropertyMetadata(string.Empty));
????????public?static?readonly?DependencyProperty?SelectedValuePathProperty?=
????????????DependencyProperty.Register(Se