Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4104650
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:08:42+00:00 2026-05-20T21:08:42+00:00

is it possible to bind an itemscontrol with canvas as template to multiple DataTemplates?

  • 0

is it possible to bind an itemscontrol with canvas as template to multiple DataTemplates?

I have 2 collections and depending on the type I would like to display a different control on my canvas.

I am not sure but I could think about a Viewmodel which has 2 ObservableCollections. For example if I would have “Shapes” and “connections” and I would like to display them both on the canvas? In case of a diagraming scenario…

I would like to do this in the mvvm manner and I am not sure if the multiple DataTemplate approach is correct but this came to my mind.
But I am still having problems to get the binding straight in my head. If I set the DataContext to the ViewModel for me it seems not possible to bind 2 collections to the items control… =(
I am also open for other ideas, too….

Is this possible? And if so, how would the binding look like an

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:08:42+00:00Added an answer on May 20, 2026 at 9:08 pm

    You can create multiple ObservableCollections and then bind your ItemsSource to a CompositeCollection which joins those collections.

    Then in your XAML you can create different DataTemplates for the respective types using the DataType property which like styles gets automatically applied if it is placed in the resources. (You can also create the composite in XAML which is shown on MSDN, if the CollectionContainers should be bound that is a bit more difficult though)

    Example code:

    ObservableCollection<Employee> data1 = new ObservableCollection<Employee>(new Employee[]
    {
        new Employee("Hans", "Programmer"),
        new Employee("Elister", "Programmer"),
        new Employee("Steve", "GUI Designer"),
        new Employee("Stefan", "GUI Designer"),
        new Employee("Joe", "Coffee Getter"),
        new Employee("Julien", "Programmer"),
    });
    ObservableCollection<Machine> data2 = new ObservableCollection<Machine>(new Machine[]
    {
        new Machine("E12", "GreedCorp"),
        new Machine("E11", "GreedCorp"),
        new Machine("F1-MII", "CommerceComp"),
        new Machine("F2-E5", "CommerceComp")
    });
    CompositeCollection coll = new CompositeCollection();
    coll.Add(new CollectionContainer() { Collection = data1 });
    coll.Add(new CollectionContainer() { Collection = data2 });
    Data = coll;
    
    <ItemsControl ItemsSource="{Binding Data}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.Resources>
            <DataTemplate DataType="{x:Type local:Employee}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}"/>
                    <TextBlock Text=" ("/>
                    <TextBlock Text="{Binding Occupation}"/>
                    <TextBlock Text=")"/>
                </StackPanel>
            </DataTemplate>
            <DataTemplate DataType="{x:Type local:Machine}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Model}"/>
                    <TextBlock Text=" - "/>
                    <TextBlock Text="{Binding Manufacturer}"/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.Resources>
    </ItemsControl>
    

    Here i use a different panel but it should be the same for a canvas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to bind the multiple commands to the button. I have a
would it be possible to bind a Java application to a Cocoa graphical interface?
Is this possible to bind a strut-1 Bean property to multiple form elements?for example,
Is it possible to bind a property to value of a setter like this
Is is possible to bind class properties to mxml components ? E.g.: I have
Is it possible to bind an add event to a collection multiple times. I
I wonder if it is possible to bind a structure element like BorderThickness.Top to
I have an ItemsControl which has a Canvas as it's ItemsPanel, the items are
Is even possible to bind an root object with super fields? I have quite
Possible Duplicate: Bind textbox to 'enter' key I have a search text box. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.