wpfbindingcomboboxselectedvaluecompositecollection

ComboBox does not select proper item when bound to CompositeCollection


I have a ComboBox bound to a collection of animals. From it I select my favourite animal. I need a static null item above the bound items. I declare it using a CompositeCollection. When the ComboBox is bound it does not select my initial favourite animal. How can I fix that? Similar problem here but still unresolved.

Observations:

I already applied these measures:

Complete C# code and XAML to demonstrate the problem:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public class Animal
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Zoo
    {
        private IEnumerable<Animal> _animals = new Animal[]
        {
            new Animal() { Id = 1, Name = "Tom" },
            new Animal() { Id = 2, Name = "Jerry" }
        };

        public Zoo(int initialId)
        {
            FavouriteId = initialId;
        }

        public int FavouriteId { get; set; }
        public IEnumerable<Animal> Animals { get { return _animals; } }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void BindComboBox(object sender, RoutedEventArgs e)
        {
            // Selecting the static item by default works.
            //DataContext = new Zoo(-1);

            // Selecting "Jerry" by default does not work.
            DataContext = new Zoo(2);
        }
    }
}

XAML

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1">

    <Window.Resources>
        <CollectionViewSource x:Key="AnimalsBridge" Source="{Binding Path=Animals}" />

        <CompositeCollection x:Key="AnimalsWithNullItem">
            <local:Animal Id="-1" Name="Pick someone..."/>
            <CollectionContainer Collection="{Binding Source={StaticResource AnimalsBridge}}" />
        </CompositeCollection>
    </Window.Resources>

    <StackPanel>
        <Button Content="Bind" Click="BindComboBox"/>

        <ComboBox x:Name="cmbFavourite"
            SelectedValue="{Binding Path=FavouriteId}"
            SelectedValuePath="Id" DisplayMemberPath="Name"
            ItemsSource="{StaticResource AnimalsWithNullItem}"/>
    </StackPanel>
</Window>

Solution

  • I dont have a solution to your problem but rather an alternative. I personally have view models dedicated to each view. I would then have a property on the view model to add the null value as required. I prever this method since it allows for better unit testing of my viewmodel.

    For 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" }); } }
    }
    

    The 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 bind to

    ComboBox x:Name="cmbFavourite"
            SelectedValue="{Binding Path=FavouriteId}"
            SelectedValuePath="Id" DisplayMemberPath="Name"
            ItemsSource="{Binding AnimalsWithNull }"/>
    

    Now you are binding directly to the source and can control the binding as normal. Also note because we are using "yield" we are not creating a new enum but rather just iterating over the existing list.