user1288039
user1288039

Reputation: 99

XAML Binding a list and filter

I have a LongListSelector Element with an ItemsSource like ItemsSource="{Binding MyList}" MyList is a ObservableCollection of MyObj. MyObj has two attributes an ID and a Name.

I'd like to filter MyList that only elements with the Name "test" will be displayed.

Can I do this in xaml code?

Thanks

Upvotes: 0

Views: 253

Answers (2)

Gerrit Fölster
Gerrit Fölster

Reputation: 964

You can't filter your list in xaml, alter your ObservableCollection in your code behind. In case you are using C# this would be:

MyList = new ObservableCollection<MyObj>(allItems.Where(x => x.Name == "test"));

Upvotes: 1

A.K.
A.K.

Reputation: 3331

Using LINQ you can do this in code

var result = MyList.Where(w => w.Name.Equals("test"));

and to this in xaml you would need CollectionViewSource

Check these helpful links

http://www.geoffhudik.com/tech/2010/10/14/wp7-in-app-searching-filtering.html

http://code.msdn.microsoft.com/wpapps/CSWP8CollectionViewSource-41c362bf

http://www.spikie.be/blog/post/2012/04/12/Filtering-collections-from-XAML-using-CollectionViewSource.aspx

Upvotes: 2

Related Questions