I want to know that when I type a first character in combo box it should display all item which are start that character and the character remains in combo box text edit.
xmlns:myControl="clr-namespace:Enrollment.Controls"
<myControl:AutoCompleteComboBox Grid.Row="7" Grid.Column="1" SelectedValue="{Binding Path=SelectedCurrentCity}" ItemsSource="{Binding Path=CurrentCityList}">
I have added a control
<UserControl x:Class="Enrollment.Controls.AutoCompleteComboBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Canvas>
<TextBox x:Name="autoTextBox" Height="25" MinWidth="240" TextWrapping="NoWrap"></TextBox>
<ComboBox MinWidth="240" x:Name="suggestionListBox" Margin="0,25,0,-25" removed="LightYellow"
Visibility="Collapsed" SelectionChanged="suggestionListBox_SelectionChanged" />
</Canvas>
</UserControl>
public Windows2()
{
fillCurrentCityList();
}
public fillCurrentCityList ()
{
CurrentCityList = new List<string>();
string tt1 = CurrStateIDcmb.SelectedValue.ToString();
string str1 = "Select CityName from mstCity where StateId = (Select StateId from mstState where StateName =@tt1)";
SqlConnection cn2 = new SqlConnection(@"Data Source=SUMIT\SQLEXPRESS;AttachDbFilename='C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Enrollment.mdf';Integrated Security=True");
SqlCommand cmd2 = new SqlCommand(str1, cn2);
cn2.Open();
cmd2.Parameters.AddWithValue("@tt1", CurrStateIDcmb.SelectedValue.ToString());
SqlDataReader dm;
try
{
dm = cmd2.ExecuteReader();
while (dm.Read())
{
CurrentCityList.Add(dm["CityName"].ToString());
}
}
catch (Exception ex)
{
}
finally
{
cn2.Close();
}
}
public partial class AutoCompleteComboBox : UserControl
{
#region Constructor
public AutoCompleteComboBox()
{
InitializeComponent();
autoTextBox.TextChanged +=
new TextChangedEventHandler(autoTextBox_TextChanged);
autoTextBox.PreviewKeyDown +=
new KeyEventHandler(autoTextBox_PreviewKeyDown);
suggestionListBox.SelectionChanged +=
new SelectionChangedEventHandler(suggestionListBox_SelectionChanged);
}
#endregion
#region Properties
public IEnumerable<string> ItemsSource
{
get
{
return (IEnumerable<string>)GetValue(ItemsSourceProperty);
}
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =DependencyProperty.Register("ItemsSource"
, typeof(IEnumerable<string>)
, typeof(AutoCompleteComboBox)
, new UIPropertyMetadata(null));
public string SelectedValue
{
get { return (string)GetValue(SelectedValueProperty); }
set { SetValue(SelectedValueProperty, value); }
}
public static readonly DependencyProperty SelectedValueProperty =
DependencyProperty.Register("SelectedValue"
, typeof(string)
, typeof(AutoCompleteComboBox)
, new UIPropertyMetadata(string.Empty));
#endregion
#region Methods
void autoTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (autoTextBox.Text.Length > 0)
{
string condition = string.Format("{0}%", autoTextBox.Text);
IEnumerable<string> results = ItemsSource.Where(delegate(string s){return s.ToLower().StartsWith(autoTextBox.Text.ToLower());
}
);
if (results.Count() > 0)
{
suggestionListBox.ItemsSource = results;
suggestionListBox.Visibility = Visibility.Visible;
}
else
{
suggestionListBox.Visibility = Visibility.Collapsed;
suggestionListBox.ItemsSource = null;
}
}
else
{
suggestionListBox.Visibility = Visibility.Collapsed;
suggestionListBox.ItemsSource = null;
}
}
void autoTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Down)
{
if (suggestionListBox.SelectedIndex < suggestionListBox.Items.Count)
{
suggestionListBox.SelectedIndex = suggestionListBox.SelectedIndex + 1;
}
}
if (e.Key == Key.Up)
{
if (suggestionListBox.SelectedIndex > -1)
{
suggestionListBox.SelectedIndex = suggestionListBox.SelectedIndex - 1;
}
}
if (e.Key == Key.Enter || e.Key == Key.Tab)
{
suggestionListBox.Visibility = Visibility.Collapsed;
e.Handled = (e.Key == Key.Enter);
}
if (e.Key == Key.Escape)
{
suggestionListBox.ItemsSource = null;
suggestionListBox.Visibility = Visibility.Collapsed;
}
}
private void suggestionListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (suggestionListBox.ItemsSource != null)
{
autoTextBox.TextChanged -= new TextChangedEventHandler(autoTextBox_TextChanged);
if (suggestionListBox.SelectedIndex != -1)
{
autoTextBox.Text = suggestionListBox.SelectedItem.ToString();
}
autoTextBox.TextChanged += new TextChangedEventHandler(autoTextBox_TextChanged);
}
}
#endregion
}
Exception in autoTextBox_TextChanged
ItemsSource shows null
http://screencast.com/t/ObdoUxAmW[
^]