I have a class and a set of IEnumerables that are using this class to give me a list in a list. (See this question's answer for details.)
Here is the code:
public IEnumerable<IEnumerable<WorkItemColumn>> EnumerateResultSet(WorkItemCollection queryResults)//DisplayFieldList displayFieldList)
{
foreach (WorkItem workItem in queryResults)
{
yield return EnumerateColumns(queryResults.DisplayFields, workItem);
}
}
public IEnumerable<WorkItemColumn> EnumerateColumns(DisplayFieldList resultSet, WorkItem workItem)
{
foreach (FieldDefinition column in resultSet)
yield return new WorkItemColumn { Name = column.Name, Value = workItem[column.Name], WorkItemForColumn = workItem};
}
And the class:
public class WorkItemColumn
{
public string Name { get; set; }
public object Value { get; set; }
public WorkItem WorkItemForColumn { get; set; }
}
I set this result to be the ItemsSource for my ListBox
Form.QueryResultListSource = EnumerateResultSet(queryResults);
The problem hits when I try to catch an event for this list box:
public void QueryResultsSelectionChanged(SelectionChangedEventArgs e)
{
+-----------------+
v |
foreach (WorkItemColumn workItemColumn in e.AddedItems) |
{ |
AddWorkItemToPad(workItemColumn.WorkItemForColumn); |
} +---------------|
| |
v |
foreach (WorkItemColumn workItemColumn in e.RemovedItems) |
{ |
RemoveWorkItemFromPad(workItemColumn.WorkItemForColumn); |
} |
|
} |
|
These items are where the problem is --------------------------------+
When I examine e.AddedItems[0] while debugging and it says its type is EnumerateColumns.
When I try cast to that type Visual Studio says (understandably) that EnumerateColumns is a method but is used like a type.
So, how can I reference this by type so I can do a foreach loop and get at the stuff inside it?
This was my updated code based on the answer:
public void QueryResultsSelectionChanged(SelectionChangedEventArgs e)
{
foreach (IEnumerable<WorkItemColumn> workItemColumns in e.AddedItems)
{
if (workItemColumns.Count() > 0)
AddWorkItemToPad(workItemColumns.First().WorkItemForColumn);
}
foreach (IEnumerable<WorkItemColumn> workItemColumns in e.RemovedItems)
{
if (workItemColumns.Count() > 0)
RemoveWorkItemFromPad(workItemColumns.First().WorkItemForColumn);
}
}