I have a XAML file tha contains a listbox and a textbox.
The listbox is filled with databinding like so:
<ListBox x:Name="lstboxNotes"
ItemsSource="{Binding Notes}"
SelectedItem="{Binding SelectedNote}"
Grid.Row="1"
></ListBox>
The textbox i have should filter out results in real time as you type in it. So for example you have 5 notes in the listbox, if you search for 'note 2' only that note would be shown, or any other note's that contain 'note 2' in their name.
I know you can do this quite easily with datatable/dataview but i do not know how to set my Itemssource to be the datatable.
I am doing filtering simply by having 3 properties:
Note so I am using https://github.com/Fody/PropertyChanged for simpler INotifyPropertyChanged
implementation in the examples ( [AlsoNotifyFor("FilteredItems")]
). You can achieve the same without this library by manually implementing the INotifyPropertyChanged
.
First of I do bind TextBox to string property:
[AlsoNotifyFor("FilteredItems")]
public string ItemsSearch { get; set; } = string.Empty;
And bind it like this.
(Note the UpdateSourceTrigger=PropertyChanged
so the binding updates with every button press.)
Text="{Binding InventorySearch, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ElementName=UserControl_Main}"
Then main collection where I do store all objects:
(You can use ObservableColletion if you plan to add and remove objects in runtime, to update UI more conviniently)
[AlsoNotifyFor("FilteredItems")]
public List<string> Items { get; set; } = new List<string>();
And at last filtered Enumerable:
public IOrderedEnumerable<string> FilteredItems =>
Items.Where(x => x.ToUpper().Contains(ItemsSearch.ToUpper()))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.