Reputation: 837
I am trying to make an auto complete textbox using method mentioned in this https://github.com/Nimgoble/WPFTextBoxAutoComplete/ link.But I am unable to make it work.
My XAML code is as follows:
<TextBox Name="searchBox"
Width="250"
HorizontalAlignment="Center"
Text="{Binding TestText, UpdateSourceTrigger=PropertyChanged}"
behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding TestItems}"
Margin="0,0,0,75" RenderTransformOrigin="0.86,0.706"
/>
This is my Code behind code :
IEnumerable<string> TestItems = new List<string>() { "John", "Mark","Doe" };
this.DataContext= TestText;
Any help will be appreciated
Upvotes: 0
Views: 1852
Reputation: 1411
Please try the below code and customize the way you like
xaml code -
<Grid>
<TextBox x:Name="txtAuto" HorizontalAlignment="Left" Height="38" Margin="181,87,0,0" PreviewKeyDown="txtAuto_KeyDown" VerticalAlignment="Top" Width="190"/>
<ListBox x:Name="lblSuggestion" HorizontalAlignment="Left" Height="60" VerticalAlignment="Top" Width="190" Margin="181,130,0,0"
Visibility="Collapsed" KeyDown="lblSuggestion_KeyDown" SelectionChanged="lblSuggestion_SelectionChanged"/>
</Grid>
Code Behind -
public partial class MainWindow : Window
{
List<string> stringCollection;
public MainWindow()
{
InitializeComponent();
stringCollection = new List<string>
{
"abc","ayr","bef","bcs","caa","lmn"
};
txtAuto.TextChanged += txtAuto_TextChanged;
}
void txtAuto_TextChanged(object sender, TextChangedEventArgs e)
{
string typedString = txtAuto.Text;
List<string> autoList = new List<string>();
autoList.Clear();
foreach (string item in stringCollection)
{
if (!string.IsNullOrEmpty(txtAuto.Text))
{
if (item.Contains(typedString))
{
autoList.Add(item);
}
}
}
if (autoList.Count > 0)
{
lblSuggestion.ItemsSource = autoList;
lblSuggestion.Visibility = Visibility.Visible;
}
else if (txtAuto.Text.Equals(""))
{
lblSuggestion.Visibility = Visibility.Collapsed;
lblSuggestion.ItemsSource = null;
}
else
{
lblSuggestion.Visibility = Visibility.Collapsed;
lblSuggestion.ItemsSource = null;
}
}
void lblSuggestion_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (lblSuggestion.ItemsSource != null)
{
lblSuggestion.KeyDown += lblSuggestion_KeyDown;
}
}
private void txtAuto_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Down)
{
lblSuggestion.Focus();
}
}
private void lblSuggestion_KeyDown(object sender, KeyEventArgs e)
{
if (ReferenceEquals(sender, lblSuggestion))
{
if (e.Key == Key.Enter)
{
txtAuto.Text = lblSuggestion.SelectedItem.ToString();
lblSuggestion.Visibility = Visibility.Collapsed;
}
if (e.Key == Key.Down)
{
e.Handled = true;
lblSuggestion.Items.MoveCurrentToNext();
}
if (e.Key == Key.Up)
{
e.Handled = true;
lblSuggestion.Items.MoveCurrentToPrevious();
}
}
}
}
Upvotes: 3
Reputation: 2686
class MyViewModel {
public IEnumerable<string> TestItems;
}
and in your constructor:
IEnumerable<string> TestItems = new List<string>() { "John", "Mark","Doe" };
this.DataContext = new MyViewModel { TestItems = TestItems };
Upvotes: 1