Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple ItemsSource collection bindings

Tags:

c#

wpf

xaml

How can I binds multiple collections of different types to an ItemsSource of an ItemsControl?

Using a single binding works fine:

<ItemsControl ItemsSource="{Binding Foo}" />

But when I try a CompositeCollection, the items from Foo aren't displayed:

    <ItemsControl>
        <ItemsControl.ItemsSource>
            <CompositeCollection>
                <CollectionContainer Collection="{Binding Foo}" />
            </CompositeCollection>
        </ItemsControl.ItemsSource>
    </ItemsControl>
like image 342
wpf Avatar asked Mar 26 '11 22:03

wpf


2 Answers

I recommend binding the ListBox to a CompositeCollection that you build in code. In this example I am using a ViewModel, but you can do the same in a code-behind as well. You can find many examples on how to implement ViewModelBase and DelegateCommand for the ViewModel via google.

Here is the breakdown of this example:

  • This example loads Customer and Person objects into two ObservableCollection containers to support modifying the collections.
  • The ListBox binds its ItemsSource to the CompositeCollection (ObjectCollection) which contains two ObservableCollections.
  • The ListBox also binds its SelectedItem to an object (SelectedObject) to support two base types.
  • The Button adds a new Person to show you can modify the CompositeCollection.
  • I added Customer and Person defintitions at the end for completeness.

Here is the View:

<Window x:Class="StackOverflow.Views.MainView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Main Window" Height="400" Width="800">
  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <ListBox Grid.Row="0" 
             SelectedItem="{Binding Path=SelectedObject}"
             ItemsSource="{Binding Path=ObjectCollection}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding FirstName}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <Button Grid.Row="1" Content="Add Person" Command="{Binding Path=AddPerson}"/>
  </Grid>
</Window>

Here is the ViewModel:

using System.Collections.Generic;
using System.Windows.Data;
using System.Windows.Input;
using ContextMenuNotFiring.Commands;
using ContextMenuNotFiring.Models;

namespace StackOverflow.ViewModels
{
  public class MainViewModel : ViewModelBase
  {
    public MainViewModel()
    {
      AddPerson = new DelegateCommand<object>(OnAddPerson, CanAddPerson);

      CollectionContainer customers = new CollectionContainer();
      customers.Collection = Customer.GetSampleCustomerList();

      CollectionContainer persons = new CollectionContainer();
      persons.Collection = Person.GetSamplePersonList();

      _oc.Add(customers);
      _oc.Add(persons);
    }

    private CompositeCollection _oc = new CompositeCollection();
    public CompositeCollection ObjectCollection
    {
      get { return _oc; }
    }

    private object _so = null;
    public object SelectedObject
    {
      get { return _so; }
      set
      {
       _so = value;
      }
    }

    public ICommand AddPerson { get; set; }
    private void OnAddPerson(object obj)
    {
      CollectionContainer ccItems = _oc[1] as CollectionContainer;
      if ( ccItems != null )
      {
        ObservableCollection<Person> items = ccItems.Collection as ObservableCollection<Person>;
        if (items != null)
        {
          Person p = new Person("AAAA", "BBBB");
          items.Add(p);
        }
      }
    }

    private bool CanAddPerson(object obj)
    {
      return true;
    }
  }
}

Here are the models:

public class Customer
{
  public String FirstName { get; set; }
  public String LastName { get; set; }

  public Customer(String firstName, String lastName)
  {
     this.FirstName = firstName;
     this.LastName = lastName;
  }

  public static ObservableCollection<Customer> GetSampleCustomerList()
  {
    return new ObservableCollection<Customer>(new Customer[4] {
            new Customer("Charlie", "Zero"), 
            new Customer("Cathrine", "One"),
            new Customer("Candy", "Two"),
            new Customer("Cammy", "Three")
        });
  }
}


public class Person
{
  public String FirstName { get; set; }
  public String LastName { get; set; }

  public Person(String firstName, String lastName)
  {
     this.FirstName = firstName;
     this.LastName = lastName;
  }

  public static ObservableCollection<Person> GetSamplePersonList()
  {
    return new ObservableCollection<Person>(new Person[4] {
            new Person("Bob", "Smith"), 
            new Person("Barry", "Jones"),
            new Person("Belinda", "Red"),
            new Person("Benny", "Hope")
        });
  }
}
like image 138
Zamboni Avatar answered Oct 20 '22 18:10

Zamboni


I believe this was answered best here: How do you bind a CollectionContainer to a collection in a view model?

It boils down to the CollectionContainer not having a DataContext. You must set the source so that it can find the DataContext and complete the binding.

like image 37
gotmug Avatar answered Oct 20 '22 19:10

gotmug