WPF Automatic completion control

Source: Internet
Author: User

Code:/Files/zhuqil/autocomplete.zip

Introduction:

In WPF, a widget that supports Automatic completion is missing. The closest control is ComboBox.Article.

Background:

When an Automatic completion control allows users to input text, the control will try its best to query a Text Selection item that the user has already entered. The most popular automatic processing is to query the beginning of the current text of this control.

How it works:

The following are the attributes of ComboBox:
  • Iseditable-This allows users to input text on this control.
  • Staysopenonedit-This will force the ComboBox to remain open during input.
  • Istextsearchenabled-This will use the default automatically completed behavior of ComboBox.
  •  

    The preceding attributes are used to control the latency query time, add new data source events, and style events to automatically complete the control..(In the autocomplete. XAML. CS file)

     

    Use this control

     

    < Window X: Class = "Gui. testwindow"
    Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
    Xmlns: CTR = "CLR-namespace: gui. Controls"
    Title = "Auto complete test"  
    Height = "200" Width = "300"  
    Loaded = "Window_loaded" >
    < Stackpanel >
    < Stackpanel. Resources >
    < Resourcedictionary
    Source = "/GUI. controls; component/styles/autocomplete. Styles. XAML"   />
    </ Stackpanel. Resources >

    < Label > Cities: </ Label >
    < CTR: AutoComplete X: Name = "Autocities"  
    Selectedvaluepath = "Cityid" Displaymemberpath = "Name"  
    Patternchanged = "Autocities_patternchanged"  
    Style =" {Staticresource autocompletecombobox} "
    Delay = "500" />  
    <! -- Can also do binding on selected value -->
    </ Stackpanel >
    </ Window >

     

    Similar to a ComboBox, the control automatically binds a specific data source using the displaymemberpath and selectvaluepath attributes.

    ///   <Summary>
    /// Occurs when the user stops typing after a delayed timespan
    ///   </Summary>
    ///   <Param name = "sender"> </param>
    ///   <Param name = "ARGs"> </param>
    Protected   Void Autocities_patternchanged ( Object Sender,
    Gui. Controls. autocomplete. autocompleteargs ARGs)
    {
    // Check
    If ( String . Isnullorempty (ARGs. pattern ))
    Args. cancelbinding =   True ;
    Else
    Args. datasource = Testwindow. getcities (ARGs. pattern );
    }

     

    We can use the patternchanged event to monitor changes in the current input data on the control.

    Interesting:

    Using mvvm mode, you can create a view model of any entity and bind it to a data source with highlighted attributes. By using a style, the highlighted part is displayed in the drop-down box.

    Note:

    The code is very simple and easy to understand. If you have any questions, please make them correct. Thank you! The red part is added by yourself.

     Original article:Http://www.codeproject.com/KB/WPF/WPF_Autocomplete.aspx

    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.