开发者

Filtering elements from a ListBox via TextBox

开发者 https://www.devze.com 2023-04-09 00:54 出处:网络
Hi have created a listbox which is bound to list of machines. As the number of machines may increase dramatically I need to somehow filter by name. For that reason I have provided a TextBox where user

Hi have created a listbox which is bound to list of machines. As the number of machines may increase dramatically I need to somehow filter by name. For that reason I have provided a TextBox where users can introduce a string to filter out. I have thought to create another list which it will bind to the view, i.e, a second list just for visualization. However, I think there must be a cleaner way to do it.

<ListBox  IsSynchronizedWithCurrentItem="True" Visibility="{Binding MachinesPanelVisibility}" 
                    ItemsSource="{Binding MachineRulesList}" SelectedIndex="{Binding ItemSelectionIndex}" />

 <TextBox HorizontalAlignment="Right" Width="162" Text="Filter..." TextWrapping="Wrap" Margin="0,44,18,13" />

On the viewModel:

public class BusinessRulesWizardViewModel : INotifyPropertyChanged
    {

public ObservableCollection<开发者_开发知识库;string> MachineRulesList
     {
       get { return _machineRulesList; }
       set
          {
            _machineRulesList = value;
             OnPropertyChanged("MachineRulesList");
          }
      }



 public BusinessRulesWizardViewModel(ISystemView systemViewManager, 
       IEventAggregator eventAggregator)
    {
         _machineRulesList = new ObservableCollection<string>();
        _systemViewManager.GetMachines(page, pageSize).ToList().ForEach(
            item => _machineRulesList.Add(item)
         );
    }


Use can use ICollectionView interface to wrap your collection with Filter property set to a predicate that uses the text entered in the TextBox. Here is an example:

<ListBox IsSynchronizedWithCurrentItem="True" Visibility="{Binding MachinesPanelVisibility}" 
         ItemsSource="{Binding MachineRulesListView}" 
         SelectedIndex="{Binding ItemSelectionIndex}" />   
<TextBox HorizontalAlignment="Right" Width="162" 
         Text="{Binding FilterText}" 
         TextWrapping="Wrap" Margin="0,44,18,13" />

-

public class BusinessRulesWizardViewModel : INotifyPropertyChanged
{

     public ObservableCollection<string> MachineRulesList
     {
       get { return _machineRulesList; }
       set
          {
            _machineRulesList = value;
             OnPropertyChanged("MachineRulesList");
          }
     }

     public string FilterText
     {
       get { return _filterText; }
       set
          {
            _filterText= value;
             OnPropertyChanged("FilterText");
            MachineRulesListView.Refresh();
          }
     }

     public ICollectionView MachineRulesListView { get; private set; }



    public BusinessRulesWizardViewModel(ISystemView systemViewManager, IEventAggregator eventAggregator)
    {
         _machineRulesList = new ObservableCollection<string>();
        MachineRulesListView = CollectionViewSource.GetDefaultView(_machineRulesList);
        MachineRulesListView.Filter = new Predicate<object>(r => ((string)r).Contains(FilterText));

        _systemViewManager.GetMachines(page, pageSize).ToList().ForEach(
            item => _machineRulesList.Add(item)
         );
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号