开发者

wpf binding in c# to a filtered list

I have recently begun development using c# and wpf. In our application we have a DataGrid object that we would like to bind to a list. However we do not want all entrys in the list to be bound, only those that meet a certain criteria. The reason that we canno开发者_如何学JAVAt bind to a seperate list (ie. bind to a list created by the applying the filter) is that we would like 2 way binding, so that when a user adds a row to the table it will be added to the overall list.

Question1: is it possible to bind to a 'filtered' list Question2: if not what is the best way to get this functionality? i.e. display only certain values but add all new entries to the overall list

thanks in advance

sam


What you want can be accomplised using the CollectionView classes. Here is how to create one with a DataTable as the source.

DataView myView = new DataView( MyDataTable ); ICollectionView cv = CollectionViewSource.GetDefaultView(myView);

You can also use ObservableCollection for a collection of your custom classes. After you create your collection view, you can set filters and sorting and grouping. In the UI, you bind to the CollectionView and you are good to go. So, for multiple views of the same data, just create different CollectionView instances and bind to those. When you update a value on the UI, the item in your store will update "automatically"

this site contains a pretty decent, but simple, example

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜