I do not have a solution to the problem, but an alternative. I personally have viewing models dedicated to each point of view. Then I would have a property on the view model to add a null value as needed. I prefer this method, as it allows me to better test the objects of my model.
In your example, add:
public class ZooViewModel { ..... public IEnumerable<Animal> Animals { get { return _animals; } } public IEnumerable<Animal> AnimalsWithNull { get { return _animals.WithDefault(new Animal() { Id = -1, Name = "Please select one" }); } } }
Magic component
public static class EnumerableExtend { public static IEnumerable<T> WithDefault<T>(this IEnumerable<T> enumerable,T defaultValue) { yield return defaultValue; foreach (var value in enumerable) { yield return value; } } }
Then in your XAML you just get attached to
ComboBox x:Name="cmbFavourite" SelectedValue="{Binding Path=FavouriteId}" SelectedValuePath="Id" DisplayMemberPath="Name" ItemsSource="{Binding AnimalsWithNull }"/>
Now you snap directly to the source and can control the binding as usual. Also note that because we use yield, we are not creating a new enumeration, but simply repeating an existing list.
source share