I am wondering if someone can share with information or good sample with filtering listboxitems based on what is typed in the textbox. Perhaps, it can be a different control that fits better to the scenario below.
In my scenario, I need to type a short string in texblock. Then, click ‘check’ button which will find the closest string values of items from the collection and show these matches in a form of the list below the textblock. Selecting any of items from shown list of items will place the selected string/item in the tetxblock. The behavior is very similar to combox box.
Finally, I need to be able to add that selected string/item that was placed in the texblock to another listbox by clicking ‘Add’ button. Any ideas are highly appreciated. Thank you in advance!
Below is my XAML code:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Class="FilterListItems.MainPage"
xmlns:local="clr-namespace:FilterListItems"
Width="640" Height="480">
<UserControl.Resources>
<local:Products x:Key="productCollection" />
<CollectionViewSource x:Key="collProducts" Source="{Binding Source={StaticResource productCollection}, Path=DataCollection}">
</CollectionViewSource>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Margin="10" Grid.Row="0">
<TextBlock Text="Enter Partial Name: " />
<TextBox Width="100" Name="txtName" />
<Button Name="btnSearch" Content="Check" Click="btn_Check" />
<Button Name="btnAdd" Content="Add" Click="btn_Add" Margin="9,0,0,0" />
</StackPanel>
<ListBox Margin="10" Grid.Row="1" Name="lstData" DisplayMemberPath="ProductName" ItemsSource="{Binding Source={StaticResource collProducts}}" Visibility="Collapsed" />
<ListBox Margin="10" Grid.Row="2" Name="2stData" />
C# to generate the collection:
public partial class MainPage : UserControl
{
public MainPage()
{
// Required to initialize variables
InitializeComponent();
}
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
//FilterData();
}
}
public class Product
{
public Product(int id, string name)
{
ProductId = id;
ProductName = name;
}
public int ProductId { get; set; }
public string ProductName { get; set; }
}
public class Products : List<Product>
{
public Products()
{
InitCollection();
}
public List<Product> DataCollection { get; set; }
List<Product> InitCollection()
{
DataCollection = new List<Product>();
DataCollection.Add(new Product(1, "aaa"));
DataCollection.Add(new Product(2, "bbb"));
DataCollection.Add(new Product(3, "ccc"));
DataCollection.Add(new Product(4, "ddd"));
DataCollection.Add(new Product(5, "eee"));
DataCollection.Add(new Product(6, "fff"));
DataCollection.Add(new Product(7, "hhh"));
DataCollection.Add(new Product(8, "ggg"));
return DataCollection;
}
}
Check out the AutoCompleteBox from wpftoolkit
also check
http://www.jeff.wilcox.name/2010/02/wpfautocompletebox/