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 395377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:26:51+00:00 2026-05-12T16:26:51+00:00

I have an ItemsControl that contains items that each has its own DataTemplate .

  • 0

I have an ItemsControl that contains items that each has its own DataTemplate. Each ViewModel class in the ItemsSource derives from a common ancestor that has a Header property.

I want to wrap each item in a Expander control, but my problem is that I don’t know how to ‘carry over’ each ‘DataTemplate’ to the DataContext of the expading part of the Expander.

In code, my problem looks like this:

VieModels:

public class VM { public string Name { get; set; } }
public class VM1 : VM { public string Description { get; set; } }
public class VM2 : VM { public string Sakkie { get; set; } }

Items property on the code-behind (because it’s simple for the purpose of this question)

    public IEnumerable<VM> Items
    {
        get
        {
            yield return new VM1() { Name = "First VM1", Description = "First VM1 Description" };
            yield return new VM2() { Name = "Vm2, nr2", Sakkie = "sakkie sakkie boeredans" } ;
            yield break;
        }
    }

The XAML of the window:

<Window.Resources>
    <DataTemplate DataType="{x:Type local:VM1}">
        <local:VM1UC />
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:VM2}">
        <local:VM2UC />
    </DataTemplate>
    <DataTemplate x:Key="DataTemplate1">
        <Expander Header="{Binding Name}">
            <ContentPresenter DataContext="{Binding DataContext, RelativeSource={RelativeSource TemplatedParent}}"/>
        </Expander>
    </DataTemplate>
</Window.Resources>
<ItemsControl ItemsSource="{Binding Items}" Background="LightCoral" ItemTemplate="{DynamicResource DataTemplate1}"/>

It looks like this, which is suprising, but understandable:

alt text http://img514.imageshack.us/img514/6937/itemscontrol.png

I am actually expecting the custom UserControls to show up in the expanded sections…

  • 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-12T16:26:52+00:00Added an answer on May 12, 2026 at 4:26 pm

    Instead of setting the DataContext, you should set the Content:

    <ContentPresenter Content="{Binding DataContext, RelativeSource={RelativeSource TemplatedParent}}"/>
    

    This will ensure the appropriate DataTemplate is resolved based on the type of the Content.

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

Sidebar

Related Questions

I have an (non-virtualized) ItemsControl that binds its ItemsSource to a ObeservableCollection of ViewModel
I have an ItemsControl with a DataTemplate that has been defined. My ItemsControl definition
I have an ItemsControl that uses a DataTemplate. The DataTemplate contains a TextBox, which
I have an ItemsControl that is bound to a collection of objects. Each object
I have ItemsControl with DataTemplate that looks like this: I want to achieve effect
Do you know any controls inherited from the ItemsControl that have horizontal orientation of
So here is the XAML that I have: <ItemsControl ItemsSource={Binding Path=Groups} ItemTemplateSelector={Binding RelativeSource={RelativeSource AncestorType=Window},
I have a Silverlight user control that contains an ItemsControl that renders a StackPanel
I have an ItemsControl which is giving me issues. It has a DataTemplate, which
I have a bound treeview with an ItemsControl that dynamically creates the treeview items

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.