locked
How can I have nested data templates? RRS feed

  • Question

  • I'm trying to get "nested data templates" and having some difficulty.

     

    Here's what I have so far -

     

            <ListBox ItemsSource="{Binding Mode=OneWay, Source={StaticResource ...}}" ItemTemplate="{StaticResource DETemplate}" />

     
    <resources...>

            <DataTemplate x:Key="DETemplate">

                    <Expander HeaderTemplate="{DynamicResource DEHeaderSummaryDT}" ContentTemplate="{StaticResource DEHeaderContentDT}" />

                    <Expander HeaderTemplateSelector="{StaticResource DEBodySummaryDTS}" ContentTemplateSelector="{StaticResource DEBodyContentDTS}"/>

                </Grid>

            </DataTemplate>

     

            <DataTemplate x:Key="DEHeaderSummaryDT">

                <Label Content="{Binding Path=Header.Id}" />

            </DataTemplate>

    ...
     
    While the binding expression of <Label Content="{Binding Path=Header.Id}" /> works in the DETemplate, it comes up empty in DEHeaderSummaryDT.
     
    What am I missing?
    Friday, April 18, 2008 9:05 PM

Answers

  • I think that this is because you did not bind to the Header and Content properties of Expander control. If you only specify the HeaderTemplate and ContentTemplate, and don't specify the Header and Content properties, the header and content will not be supplied on the control.

     

    Code Snippet

    <Window

       x:Class="ForumProjects.MainWindow"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       x:Name="Window">

        <Window.Resources>

            <DataTemplate x:Key="NameTemplate">

                <TextBlock Text="{Binding}"/>

            </DataTemplate>

            <DataTemplate x:Key="IntroductionTemplate">

                <TextBlock Text="{Binding}"/>

            </DataTemplate>

            <DataTemplate x:Key="HeaderTemplate">

                <TextBlock Text="{Binding}"/>

            </DataTemplate>

            <DataTemplate x:Key="BodyTemplate">

                <TextBlock Text="{Binding}"/>

            </DataTemplate>

            <DataTemplate x:Key="MessageTemplate">

                <StackPanel>

                    <Expander HeaderTemplate="{StaticResource NameTemplate}"

                            ContentTemplate="{StaticResource IntroductionTemplate}"

                            Header="{Binding Sender.Name}" Content="{Binding Sender.Introduction}"/>

                    <Expander HeaderTemplate="{StaticResource HeaderTemplate}"

                            ContentTemplate="{StaticResource BodyTemplate}"

                            Header="{Binding Header}" Content="{Binding Body}"/>

                </StackPanel>

            </DataTemplate>

        </Window.Resources>

        <ListBox ItemsSource="{Binding Messages, ElementName=Window}"

                ItemTemplate="{StaticResource MessageTemplate}"/>

    </Window>

    namespace ForumProjects

    {

        public partial class MainWindow : Window

        {

            public MainWindow()

            {

                InitializeComponent();

            }

            public List<Message> Messages

            {

                get

                {

                    return new List<Message>() {

                        new Message("Person A","Introduction A","Header 1","Content 1"),

                        new Message("Person B","Introduction B","Header 2","Content 2")};

                }

            }

        }

        public class Message

        {

            public Message(string name, string introduction, string header, string body)

            {

                this.Sender = new Person() { Name = name, Introduction = introduction };

                this.Header = header;

                this.Body = body;

            }

            public Person Sender { get; set; }

            public string Header { get; set; }

            public string Body { get; set; }

        }

        public class Person

        {

            public string Name { get; set; }

            public string Introduction { get; set; }

        }

    }

     

     

    Best Regards,

    Wei Zhou

    Monday, April 21, 2008 4:45 AM

All replies

  • I believe it's a bug of ContentPresenter. I've submitted a bug report here:

    http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=338874

    Saturday, April 19, 2008 3:41 AM
  • I think that this is because you did not bind to the Header and Content properties of Expander control. If you only specify the HeaderTemplate and ContentTemplate, and don't specify the Header and Content properties, the header and content will not be supplied on the control.

     

    Code Snippet

    <Window

       x:Class="ForumProjects.MainWindow"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       x:Name="Window">

        <Window.Resources>

            <DataTemplate x:Key="NameTemplate">

                <TextBlock Text="{Binding}"/>

            </DataTemplate>

            <DataTemplate x:Key="IntroductionTemplate">

                <TextBlock Text="{Binding}"/>

            </DataTemplate>

            <DataTemplate x:Key="HeaderTemplate">

                <TextBlock Text="{Binding}"/>

            </DataTemplate>

            <DataTemplate x:Key="BodyTemplate">

                <TextBlock Text="{Binding}"/>

            </DataTemplate>

            <DataTemplate x:Key="MessageTemplate">

                <StackPanel>

                    <Expander HeaderTemplate="{StaticResource NameTemplate}"

                            ContentTemplate="{StaticResource IntroductionTemplate}"

                            Header="{Binding Sender.Name}" Content="{Binding Sender.Introduction}"/>

                    <Expander HeaderTemplate="{StaticResource HeaderTemplate}"

                            ContentTemplate="{StaticResource BodyTemplate}"

                            Header="{Binding Header}" Content="{Binding Body}"/>

                </StackPanel>

            </DataTemplate>

        </Window.Resources>

        <ListBox ItemsSource="{Binding Messages, ElementName=Window}"

                ItemTemplate="{StaticResource MessageTemplate}"/>

    </Window>

    namespace ForumProjects

    {

        public partial class MainWindow : Window

        {

            public MainWindow()

            {

                InitializeComponent();

            }

            public List<Message> Messages

            {

                get

                {

                    return new List<Message>() {

                        new Message("Person A","Introduction A","Header 1","Content 1"),

                        new Message("Person B","Introduction B","Header 2","Content 2")};

                }

            }

        }

        public class Message

        {

            public Message(string name, string introduction, string header, string body)

            {

                this.Sender = new Person() { Name = name, Introduction = introduction };

                this.Header = header;

                this.Body = body;

            }

            public Person Sender { get; set; }

            public string Header { get; set; }

            public string Body { get; set; }

        }

        public class Person

        {

            public string Name { get; set; }

            public string Introduction { get; set; }

        }

    }

     

     

    Best Regards,

    Wei Zhou

    Monday, April 21, 2008 4:45 AM
  • Thanks so much. Now that I know the answer, it's pretty obvious!

    Monday, April 21, 2008 2:43 PM
  • Thanks REALLY MUCH!
    Thursday, December 10, 2015 1:23 AM