private void CoerceSelectedItem(ISpriteCategory category, [CallerMemberName] string callerName = null) { if (supressEvents) { return; } string propName = "null"; if (GetBindingExpression(ItemsSourceProperty) != null) { propName = GetBindingExpression(ItemsSourceProperty).ResolvedSourcePropertyName; } Console.WriteLine($"{propName} SpriteCategoryComboBox.{callerName} coerce"); supressEvents = true; ISpriteCategory source = ItemsSource; if (source == null) { category = null; } else if (category != null && source.TryGetValue(category.Id, out ISpriteCategory newCategory)) { category = newCategory; } else { category = (ISpriteCategory)source.List.FirstOrDefault(); } SelectedItem = category; SelectedItemInternal = category; supressEvents = false; }
/// <summary> /// Tries to get the category with the specified Id in the category. /// </summary> /// <param name="id">The Id of the category to get.</param> /// <param name="value">The output category if one was found, otherwise null.</param> /// <returns>True if a category with the Id was found, otherwise null.</returns> /// /// <exception cref="ArgumentNullException"> /// <paramref name="id"/> is null. /// </exception> public bool TryGetValue(object id, out ISpriteCategory value) { if (id == null) { throw new ArgumentNullException(nameof(id)); } value = (ISpriteCategory)List.Find(e => e.Id.Equals(id)); return(value != null); }