C # Implementation of combobox_c# tutorial with search function

Source: Internet
Author: User

The

ComboBox with a search is the Itemsource that gives ComboBox a dependent property, and then the ComboBox bound data source through whether the data source contains the value to query.

public class Editcombobox:combobox {private bool t = true;//First gain focus logo bit private observablecollection<object  > bindinglist = new observablecollection<object> ()//Data source binding list private String edittext = "";//Edit Text content/// <summary>///Registration Dependency event///</summary> public static readonly DependencyProperty Itemssourceproperty New = Dependencyproperty.register ("Myitemssource", typeof (IEnumerable), typeof (Editcombobox), new
    Frameworkpropertymetadata (New Propertychangedcallback (valuechanged)); <summary>///Data source change, add data source to binding data source///</summary>///<param name= "D" ></param>// /<param name= "E" ></param> private static void ValueChanged (DependencyObject D, Dependencypropertychangede
      Ventargs e) {Editcombobox ECB = d as editcombobox;
      Ecb.bindingList.Clear (); Traversal loop operation foreach (Var item in ECB.)
      Myitemssource) {ECB.BINDINGLIST.ADD (item); }
    }
    ///<summary>///set or get ComboBox data source///</summary> public IEnumerable Myitemssource {gets
      {return (IEnumerable) GetValue (itemssourcepropertynew);
        set {if (value = = null) clearvalue (itemssourcepropertynew);
      else SetValue (itemssourcepropertynew, value); }///<summary>///rewrite initialization///</summary>///<param name= "E" ></param> PR otected override void OnInitialized (EventArgs e) {base.
      OnInitialized (e); This.
      Iseditable = true; This.
      istextsearchenabled = false; This.
    ItemsSource = BindingList;
    ///<summary>///dropdown box gets focus, first search text edit box///</summary>///<param name= "E" ></param>
      protected override void OnGotFocus (RoutedEventArgs e) {if (t) Findtextbox (this);
    else T = false; ///<summary>///Search Edit text box, add text Change event///</summary>///<param name= "obj" ></param> private void Findtextbox (DependencyObject obj) { for (int i = 0; i < visualtreehelper.getchildrencount (obj); i++) {DependencyObject child = Visualtre
        Ehelper.getchild (obj, i); if (Child!=null && child is textbox) {//Register text Change event (child as TextBox).
        TextChanged + = editcombobox_textchanged;
        else {findtextbox (child); }}///<summary>///text changes, dynamic control dropdown data source///</summary>///<param name= "Sender" &G t;</param>///<param name= "E" ></param> private void Editcombobox_textchanged (object sender, Tex
      Tchangedeventargs e) {textbox TB = sender as TextBox; if (TB. isfocused) {this.
        Isdropdownopen = true; if (EditText = = this.
        Text) return; EditText = this.
        Text;
      Setlist (EditText); }
    }
    /<summary>///combo box closed, data source restore///</summary>///<param name= "E" ></param> protected override void Ondropdownclosed (EventArgs e) {base.
      Ondropdownclosed (e);
      if (Myitemssource = null) return;
      foreach (var item in Myitemssource) {if (!bindinglist.contains (item)) Bindinglist.add (item); }///<summary>///filter Eligible data items, add to the data source item///</summary>///<param name= "TXT" &GT;&L
        t;/param> private void setlist (string txt) {try {string temp1 = ' ";
        String temp2 = "";
        if (Myitemssource = null) return; foreach (var item in myitemssource) {Temp1 = Item. GetType (). GetProperty (this. DisplayMemberPath). GetValue (item, NULL).
          ToString (); if (string. IsNullOrEmpty (this.
          SelectedValuePath)) {temp2 = ""; else {Temp2 = Item.GetType (). GetProperty (this. SelectedValuePath). GetValue (item, NULL).
          ToString (); } if (Temp1. Contains (TXT) | | Temp2.
          StartsWith (TXT)) {if (!bindinglist.contains (item)) Bindinglist.add (item);
          else if (Bindinglist.contains (item)) {Bindinglist.remove (item); '} ' catch (Exception ex) {MessageBox.Show (ex).
      ToString ());
 }
    }
  }

The calling method is to bind the data source to the Myitemssource, and the rest is the same as the original ComboBox usage.

Copy Code code as follows:
<local:editcombobox myitemssource= "{Binding prolist,mode=twoway}" selecteditem= "{Binding Selpro,mode=twoway}" Selectedvaluepath= "Id" displaymemberpath= "Name"/>

Effect Demo

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.