how do i filter the current items list of a listbox?-Collection of common programming errors
I have a listbox that is populated with a list of List items. The collection is completely unfiltered. What I am trying to accomplish is creating the ability to filter the listbox and reorder the listbox based on two different criteria. The first being filter by status. I have a combobox that contains all of the different statuses. I have accomplished this like so:
public void ConstrainList()
{
if (((Lut_status)cboStatus.SelectedItem).ID != 8)
{
lbxHoldList.ItemsSource = allHolds.Where(a => a.FullHoldRecord.HoldStatus == ((Lut_status)cboStatus.SelectedItem).ID);
}
else
{
lbxHoldList.ItemsSource = allHolds.Where(a => a.FullHoldRecord.HoldStatus != 7);
}
}
This is a method that runs when the combobox selectionchanged event fires. This works fine. Where I am getting muddled up is I need to resort the constrained list based on one of 3 criteria using radio buttons. So I was thinking I could just grab the ItemsSource object, sort it with linq and then reassign it doing something like:
lbxHoldList.ItemsSource = ((List)lbxHoldList.ItemsSource).OrderBy(a => a.FullHoldRecord.DateOpened);
This works the first timem, but starts crashing with each subsequent call. I don’t feel like this is the best way to go about this. Can someone help me either fix this approach or show me a better way to do this? Thanks