带有复选框的WPF ComboBox和带搜索字段的文本框

我已经完成了这个post

寻找带有复选框的WPF ComboBox,并尝试在DataTemplate包含TextBox ,但在运行应用程序时无法查看TextBox 。 我创建了一个UserControl ,如下所示

      ****                                              

这是代码

  using System; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; using System.Linq; using System.Collections.Generic; using System.Text; using System.ComponentModel; using System.Runtime.CompilerServices; namespace MyProj.UserControls { ///  /// Interaction logic for MultiSelectComboBox.xaml ///  public partial class MultiSelectComboBox : UserControl { private ObservableCollection _nodeList; public MultiSelectComboBox() { InitializeComponent(); _nodeList = new ObservableCollection(); } #region Dependency Properties public event EventHandler CheckBoxClick; public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(Lexicon), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnItemsSourceChanged))); public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(Lexicon), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnSelectedItemsChanged))); public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MultiSelectComboBox), new UIPropertyMetadata(string.Empty)); public static readonly DependencyProperty DefaultTextProperty = DependencyProperty.Register("DefaultText", typeof(string), typeof(MultiSelectComboBox), new UIPropertyMetadata(string.Empty)); public Lexicon ItemsSource { get { return (Lexicon)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } public Lexicon SelectedItems { get { return (Lexicon)GetValue(SelectedItemsProperty); } set { SetValue(SelectedItemsProperty, value); } } public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } public string DefaultText { get { return (string)GetValue(DefaultTextProperty); } set { SetValue(DefaultTextProperty, value); } } #endregion #region Events private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { MultiSelectComboBox control = (MultiSelectComboBox)d; control.DisplayInControl(); } private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { MultiSelectComboBox control = (MultiSelectComboBox)d; control.SelectNodes(); control.SetText(); } private void CheckBox_Click(object sender, RoutedEventArgs e) { CheckBox clickedBox = (CheckBox)sender; if (clickedBox.Content.ToString() == "All") { if (clickedBox.IsChecked.Value) { foreach (Node node in _nodeList) { node.IsSelected = true; } } else { foreach (Node node in _nodeList) { node.IsSelected = false; } } } else { int _selectedCount = 0; foreach (Node s in _nodeList) { if (s.IsSelected && s.Text != "All") _selectedCount++; } if (_selectedCount == _nodeList.Count - 1) _nodeList.FirstOrDefault(i => i.Text == "All").IsSelected = true; else _nodeList.FirstOrDefault(i => i.Text == "All").IsSelected = false; } SetSelectedItems(); SetText(); if (this.CheckBoxClick != null) this.CheckBoxClick(this, e); } #endregion #region Methods private void SelectNodes() { foreach (KeyValuePair keyValue in SelectedItems) { Node node = _nodeList.FirstOrDefault(i => i.Text == keyValue.Key); if (node != null) node.IsSelected = true; } } private void SetSelectedItems() { if (SelectedItems == null) SelectedItems = new Lexicon(); SelectedItems.Clear(); foreach (Node node in _nodeList) { if (node.IsSelected && node.Text != "All") { if (this.ItemsSource.Count > 0) SelectedItems.Add(node.Text, node.Id); } } } //private void DisplayInControl() //{ // _nodeList.Clear(); // foreach (KeyValuePair keyValue in this.ItemsSource) // { // Node node = null; // if (this.ItemsSource.Count == 1) // node = new Node(keyValue.Key); // else // node = new Node(keyValue.Key); // _nodeList.Add(node); // if (this.ItemsSource.Count == 1) SetSelectedItems(); // } // MultiSelectCombo.ItemsSource = _nodeList; // if (this.ItemsSource.Count == 1) SetText(); // else // this.Text = "--Select--"; //} private void DisplayInControl() { _nodeList.Clear(); if (this.ItemsSource.Count > 0) _nodeList.Add(new Node("All", false, -1)); foreach (KeyValuePair keyValue in this.ItemsSource) { Node node = new Node(keyValue.Key, false, keyValue.Value); _nodeList.Add(node); } MultiSelectCombo.ItemsSource = _nodeList; this.Text = "--Select--"; } private void SetText() { if (this.SelectedItems != null) { StringBuilder displayText = new StringBuilder(); foreach (Node s in _nodeList) { if (s.IsSelected == true && s.Text == "All") { displayText = new StringBuilder(); displayText.Append("All"); break; } else if (s.IsSelected == true && s.Text != "All") { displayText.Append(s.Text); displayText.Append(','); } } this.Text = displayText.ToString().TrimEnd(new char[] { ',' }); } // set DefaultText if nothing else selected if (string.IsNullOrEmpty(this.Text)) { this.Text = this.DefaultText; } } #endregion } public class Node : INotifyPropertyChanged { private string _text; private bool _isSelected; private int _id; #region ctor public Node(string text, bool selected, int id) { Text = text; IsSelected = selected; Id = id; } #endregion #region Properties public int Id { get { return _id; } set { _id = value; NotifyPropertyChanged("Id"); } } public string Text { get { return _text; } set { _text = value; NotifyPropertyChanged("Text"); } } public bool IsSelected { get { return _isSelected; } set { _isSelected = value; NotifyPropertyChanged("IsSelected"); } } #endregion public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } } 

那么有人可以让我知道我需要做什么更改才能在CheckBoxes上显示TextBox ,这样当我输入一些文本时它应该过滤并给我结果

修改ControlTemplateScrollViewer元素以包含TextBox

                

ItemTemplate不需要任何TextBox