簡體   English   中英

在WPF中搜索在列表框中選擇項目

[英]Select item in ListBox with search in WPF

如何通過將文本插入到TextBox中來選擇ListBox中的項目?

我在控件中有兩個元素: ListBox包含用於搜索的對象, TextBox用於插入要搜索的文本。

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

列表ListOfItems包含Bar類型的對象。 我想按字段name搜索項目:

class Bar
{
     public string name;
     ...
}

用戶可以在TextBox插入文本,然后從ListBox選擇相應的項目。

通過搜索列表並將selectecUnit設置為找到的列表:

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

基本思想是查找搜索字符串更改,並更新選定的Bar項。 綁定將完成其余的工作。

假設Bar看起來像這樣:

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

    // ...
}

您可以創建此視圖模型類:

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
}

並以這種方式使用它:

<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>

您可以綁定所選項目(直接綁定或同步綁定)

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

與c.tor一起在ViewModel中的研究結果

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

和一個委托命令來找到項目

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM