【问题标题】:c# searching in textbox from listboxc# 从列表框中搜索文本框
【发布时间】:2018-06-07 12:26:23
【问题描述】:

我有一个带有人名的文本框和列表框。我想在文本框中输入名称,它应该更新列表框信息。但我不知道该怎么做。我该怎么做?

我想在文本框中写入内容时过滤列表框行。

MainWindow.xaml 代码:

<ListBox HorizontalAlignment="Left" Height="127" ItemsSource="{Binding Persons}" Name="PersonLstbox"
             Margin="10,22,0,0" VerticalAlignment="Top" Width="197">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding FirstName}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

文本框代码:

<TextBox Name="searchpersonbx" HorizontalAlignment="Left" Height="23" Margin="420,150,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="searchpersonbx_TextChanged"/>

MainWindow.xaml.cs 代码:

    private void searchpersonbx_TextChanged(object sender, TextChangedEventArgs e)
    {

    }

【问题讨论】:

  • 绑定的集合是什么
  • it should update the Listbox information 是什么意思?它应该如何更新 Listbox 信息?
  • 当我在文本框中写入内容时,列表框上的信息会更新。
  • 您的意思是要根据文本框的内容过滤列表框行吗?
  • 你看到这个问题stackoverflow.com/questions/15358113/wpf-filter-a-listbox了吗?可能与您的问题有关。

标签: c# wpf search listbox


【解决方案1】:

你可以修改你的代码如下:-

这里我使用StartsWith() 来获取指定顺序的所有字符串

您的用户名列表

List<string> userName = new List<string>();

TextChanged 事件

private void searchpersonbx_TextChanged(object sender, TextChangedEventArgs e)
{
   string text = searchpersonbx.Text;
   List<string> filteredUserName = userName.Select(x => x.StartsWith(text)).ToList();
   listBox.ItemsSource = filteredUserName;
}

【讨论】:

    猜你喜欢
    • 2011-06-20
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多