C# searchbox in wpf application

I have a problem with searchbox in a C# wpf application project. I want to do a searchbox and I do not know how to get it working.

It should work like this, when I enter something in textbox, it automatically updates the listbox with the word or part of the word I wrote in the textbox.

Here is my code in MainWindow.xaml; first is my TextBox where I want to enter a search value I want to see in the listbox

<TextBox Height="27" HorizontalAlignment="Left" Margin="69,92,0,0"
        Name="txtNameToSearch" VerticalAlignment="Top" Width="174"
        TextChanged="txtNameToSearch_TextChanged"/>

This is my listbox where I want show the search results:

<ListBox Name="BoozeList" ItemsSource="{Binding Boozes}"
             Margin="10,124,0,10"
             HorizontalAlignment="Left"
             ScrollViewer.VerticalScrollBarVisibility="Visible"
              Width="233" Background="#FFCFFF90">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Path=Name}" />
                    <TextBlock Text="{Binding Path=Quantity}" />
                    <TextBlock Text="{Binding Path=UnitPrice}" />

                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

This is my code in MainWindow.xalm.cs:

private void txtNameToSearch_TextChanged(object sender, TextChangedEventArgs e) //seachbox search fuc from listbox
{
    if (e.KeyChar == (char)13)
    {
        DataView dv = dt.DefaultView;
        dv.RowFilter = string.Format("Boozename like '%{0}%'", txtSearch.Text);
        dataGridView.DataSource = dv.ToTable();
    }
}

1 answer

  • answered 2018-01-14 11:03 Bijan

    change the row filter string to this:

    private void txtNameToSearch_TextChanged(object sender, TextChangedEventArgs e) //seachbox search fuc from listbox
    {
        if (e.KeyChar == (char)13)
        {
            DataView dv = dt.DefaultView;
            dv.RowFilter = $"Column1 like '%{txtSearch.Text}%'";
            dataGridView.DataSource = dv.ToTable();
        }
    }