简体   繁体   English

在WPF中搜索在列表框中选择项目

[英]Select item in ListBox with search in WPF

How can I select item in ListBox by inserting text to TextBox? 如何通过将文本插入到TextBox中来选择ListBox中的项目?

I have two elements in control: ListBox which contains object for searching and TextBox used for inserting text to search. 我在控件中有两个元素: ListBox包含用于搜索的对象, TextBox用于插入要搜索的文本。

<StackPanel>
    <TextBox x:Name="textForSearchinInList"/>
    <ListBox ItemsSource="{Binding ListOfItems}" x:Name="listOfItems"
             SelectedItem="{Binding SelectedUnit, Mode=TwoWay}">
        ...
    </ListBox>
</StackPanel>

List ListOfItems contains objects of type Bar . 列表ListOfItems包含Bar类型的对象。 And I want to search item by field name : 我想按字段name搜索项目:

class Bar
{
     public string name;
     ...
}

User can insert a text to the TextBox and corresponding item would be selected from the ListBox . 用户可以在TextBox插入文本,然后从ListBox选择相应的项目。

通过搜索列表并将selectecUnit设置为找到的列表:

SelectedUnit = ListOfItems.FirstOrDefault(x=>x.name == testForSearchingInList.Text);

The basic idea is to look for search string changes, and update selected Bar item. 基本思想是查找搜索字符串更改,并更新选定的Bar项。 Binding will do the rest. 绑定将完成其余的工作。

Assuming, that Bar looks like this: 假设Bar看起来像这样:

public sealed class Bar
{
    public string Name { get; set; }

    // ...
}

you can create this view model class: 您可以创建此视图模型类:

public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        BarItems = new[]
        {
            new Bar { Name = "Dog" },
            new Bar { Name = "Cat" },
            new Bar { Name = "Mouse" },
        };
    }

    public string SearchString
    {
        get { return searchString; }
        set
        {
            if (searchString != value)
            {
                searchString = value;
                SelectedBar = BarItems.FirstOrDefault(_ => !string.IsNullOrEmpty(_.Name) && _.Name.IndexOf(searchString, StringComparison.CurrentCultureIgnoreCase) >= 0);
                OnPropertyChanged();
            }
        }
    }
    private string searchString;

    public Bar SelectedBar
    {
        get { return selectedBar; }
        set
        {
            if (selectedBar != value)
            {
                selectedBar = value;
                OnPropertyChanged();
            }
        }
    }
    private Bar selectedBar;

    public IEnumerable<Bar> BarItems { get; }

    // INPC implementation is omitted
}

and use it this way: 并以这种方式使用它:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1">

    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>

    <StackPanel>
        <TextBox Text="{Binding SearchString, UpdateSourceTrigger=PropertyChanged}"/>
        <ListBox ItemsSource="{Binding BarItems}" SelectedItem="{Binding SelectedBar}">
            <ListBox.ItemTemplate>
                <DataTemplate DataType="{x:Type local:Bar}">
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Window>

You can bind the selected item (directly or with synchronization ) 您可以绑定所选项目(直接绑定或同步绑定)

<ListBox SelectedItem="FoundItem" IsSynchronizedWithCurrentItem="True"  

to the result of the research in the ViewModel, with a c.tor 与c.tor一起在ViewModel中的研究结果

public YourViewModel()
        {
            IList<Bar> bars = GetBars().ToList();
            _barView = CollectionViewSource.GetDefaultView(bars);
            _barView.CurrentChanged += BarSelectionChanged;

and a delegate command to find the item 和一个委托命令来找到项目

 FoundItem = ListOfItems.FirstOrDefault( x => x.name // etc..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM