【问题标题】:Select item in ListBox with search in WPF在 WPF 中使用搜索选择 ListBox 中的项目
【发布时间】:2016-09-05 12:35:59
【问题描述】:

如何通过向 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 中选择。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    基本思路是查找搜索字符串的变化,并更新选中的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>
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        你可以绑定选中的项(直接或者用synchronization

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

        到 ViewModel 中的研究结果,带有 c.tor

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

        以及一个用于查找项目的委托命令

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

        【讨论】:

          猜你喜欢
          • 2018-01-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-01
          相关资源
          最近更新 更多