Getting around lack of ItemBound on a ListView in WPF

I recently was working on a project where I needed to bind some data to a ListView in WPF. This is pretty easy, and using my Dataset, took really no work at all. But when trying to figure out some simple ways to update my ListView on each item being bound (where you would usually use a OnItemBound event in other controls), this (and nothing like it) really exists in a WPF ListView control.

There were a few workarounds for this. I could update how my DataSet was being generated, and just bind those ‘special’ controls in the ListView in these new fields, or use a List that I found in this post. While updating the DataSet would be nice, using a List may let me knock out two birds with one stone (due to some other requirements).

So I set forth mapping the DataSet to my List. This is as simple as doing something like…

List theItems = new List();

foreach(DataRow dr in ds.Tables[0].Rows)
{
MyItem tmpItem = new MyItem();
tmpItem.Value1 = dr["value1"].tostring();
tmpItem.Value2 = dr["value2"].tostring();

theItems.add(tmpItem);
}

I can then map this collection to my ListView, rather than the DataSource itself. All I have to do in my XAML then is just update my ‘DisplayMemberBinding=”{Binding value1}‘ to point to the new item in the list ‘DisplayMemberBinding=”{Binding Value1}‘. If one of those is a bool, and I have that mapped to a Checkbox or RadioButton, its a little easier for me to manage (being so used to having the option to do an OnItemBound method to handle some of the same stuff). My bigger concern of needing this was to convert values on the fly. For example, if something is returning any value (int or null), id like to map that to True or False instead. This is a really simple example, but just an idea of something you could do if you needed this extra functionality!

[ad#postad]

Listening To: ’15 Step’ by Radiohead

Advertisements