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