# [.net] ListView conditional data binding

This topic is 3532 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Alright, here's the setup: I'm using a ListView specified in XAML (WPF) and binding it to an ObservableCollection stored in an object. This bit works fine. Edit: Using C# 3.0. Which means I have full access to Linq, WPF and company. And the problem: The ideal behavior would be that only *certain* items in the collection would be bound to the ListView and show up. For example, say my collection contained named critters, and I wanted the ListView only to show critters with the name "Alfonse." Anyone know of a way to do this? Any special XAML black magic I can use? Tangential to this problem, I have a ComboBox that I only want to be bound to a single item in my array. Same issue as above, in essence, I just know that there will be only one of these items. I've tried implementing an IValueConverter that converts to a more specialized form of the data, but that has the issue that when doing the reverse-conversion, I'd lose all the data not in the ListView. Plus, having to convert back to the 'blob' I have is icky. And here's the implementation details, for more insight: I have a class that I want to be able to annotate with 'Extra Data' that gets written to its XML-serialized file. This extra data really only consists of a series of key-value strings (with a 'type' string for a little more info); it's mostly used for debugging purposes and tools-side stuff. I want to keep it generic - I don't want to put in 'special' kinds of extra data or do something like create a IExtraData interface and have all the special kinds of extra data implement that (which, as an aside, was the first path I went down before it turned into an overly-complex mess). This all works good and fine, but it's made GUI-crafting a pain, since at least one critical dialog has to interact with the extra data. Using another GUI solution really isn't an option - we have a ton of code using this system, and there are deadlines in play. So are there any C# constructs I'm missing that might make this a little less painful? Or is there an ability to create new objects in XAML/invoke methods/grab specific entries from a dictionary? I could easily enough write a Navigator class that navigates the list and yanks out only the necessary information, but I don't want to litter the code with a bunch of navigators. Cheers, --Brian

##### Share on other sites
To filter the collection, you can use the ICollectionView interface.

This is just off the top of my head:

    ICollectionView view = CollectionViewSource.GetDefaultView(myCollection);    view.Filter = x => x.IsWhatever;    MyListViewCtrl.ItemsSource = view;

I'm not sure I understand the problem with the ComboBox, you normally bind a ComboBox to a collection of items, but if you just want to bind to one of them, you can use the method above to filter out everything else.

Finally, you can implement the ICustomTypeDescriptor interface to ease data-binding to objects that have "generic" properties like you describe (if I understand what you're asking correctly, that is...)

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631708
• Total Posts
3001840
×