4

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:

  • Binding to the static item works i.e. if I don't have an initial favourite animal the static item gets selected.
  • The problem disappears if the static item is removed. Of course this would make the CompositeCollection and this whole question obsolete.

I already applied these measures:

  • A CollectionContainer cannot bind directly to a property as outlined here.
  • The composite collection is also moved to a static resource as suggested here.

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>

1 Answer 1

4

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.

2
  • +1 for the black magic of yield. I still had to use a converter to correctly set a ListViewItem's property from the selected value of the ComboBox though.
    – helloserve
    Dec 19, 2013 at 18:56
  • Marking as answer because a solution doesn't seem to exist. Items can be prepended efficiently by built-in methods too: new[] { new Animal() { ... } }.Concat(_animals)
    – idilov
    Sep 18, 2014 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.