wpf datagrid readonly jump to row while typing
In a readonly datagrid where tabular data is displayed, I want to have the (out of the box) functionality to jump 开发者_开发百科to a row when an user types in some characters.
Say the following records are available:
- ... Some data ...
- Office supplies
- Orders
- ... More data ...
When an user types in the "O", the row with ID "Office supplies" should be selected. When the user types an "r" after that, the "Orders" row should be selected.
My question actually is: is there an out of the box solution for this, or do I have to create something custom?
There is no "out of the box" functionality to do this, you have to create your own.
I suggest:
- listening to the keyboard (when the focus is on the DG for instance)
- storing the stricken keys inside a buffer
- after each keyStroke, looping every cell in your dataSource to look fo the first match (depending on the data size, this might be VERY costly as you have to make a string compare for each cell, for every single cell in your dataSource in the worst case...)
- set the currentCell of the dataGrid to the given row/column position (there are a few ways to do this)
so this is not that complicated, but it requires some coding and most of all, this might turn into a performance nightmare if you have huge dataSets.
You will pretty much have to create a textbox for the search input then filter the collection your DataGrid is bound to with LINQ (Or any other way you like) and clone the resulting objects into another collection. Finally, re-bind the DataGrid to those cloned objects and re-set the DataContext of the window so the DataGrid becomes aware of the binding change. Lastly, implement a check so if the user leaves and empty textbox, you re-bind to the original collection and re-set the DataContext just again.
Hope this helps ;)
精彩评论