private static void ResynchList(ListBox listBox)
{
if (listBox != null)
{
listBox.SetValue(IsResynchingPropertyKey, true);
IList list = GetSelectedItemsSource(listBox);
if (listBox.SelectionMode == SelectionMode.Single)
{
listBox.SelectedItem = null; if (list != null)
{
if (list.Count > 1)
{
// There is more than one item selected, but the listbox is in Single selection mode
throw new InvalidOperationException("ListBox is in Single selection mode, but was given more than one selected value.");
}
if (list.Count == 1)
listBox.SelectedItem = list[0];
}
}
else
{
listBox.SelectedItems.Clear();
if (list != null)
{
foreach (object obj in listBox.Items)
{
if (list.Contains(obj))
listBox.SelectedItems.Add(obj);
}
}
}
listBox.SetValue(IsResynchingPropertyKey, false);
}
}