views:

474

answers:

0

Good Day,

I have a datagrid in Silverlight that filters data when the user clicks on a cell, then clicks on an button icon in the column header. I know how to determine which column the user selected as well as what the selection was.

My datagrid is bound to an ObservableCollection of T where T is a custom businss object called Equipment.

Equipment contains the following properties:

EquipmentCategory EquipmentName EqupmentType CheckoutName Department

And here is the code snippet the filters the data correctly

Equipment itemToFilter = (Equipment)theGrid.SelectedItem;

// Convert custom collection to an IEnumerable<T>
IEnumerable<Equipment> source = (IEnumerable<Equipment>)_data;

// Extract the field name from the Equipment class
string fieldName = theGrid.CurrentColumn.ClipboardContentBinding.Path.Path;

// So I can use an extension method to filter data
if (fieldName == "EquipmentCategory")
    theGrid.ItemsSource = source.Where((Equipment eq) => eq.EquipmentName == itemToFilter.EquipmentCategory);    
if (fieldName == "EquipmentName")
    theGrid.ItemsSource = source.Where((Equipment eq) => eq.EquipmentName == itemToFilter.EquipmentName);
if (fieldName == "EquipmentType")
    theGrid.ItemsSource = source.Where((Equipment eq) => eq.EquipmentType == itemToFilter.EquipmentType);
if (fieldName == "CheckoutName")
    theGrid.ItemsSource = source.Where((Equipment eq) => eq.CheckoutName== itemToFilter.CheckoutName);
if (fieldName == "Department")
    theGrid.ItemsSource = source.Where((Equipment eq) => eq.Department== itemToFilter.Department);

Here, you may be able to see where my problem is. I would rather try to avoid hard wiring the names if possible. Is there a dynamic way to do this?

TIA,

coson