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

  • Home
  • SEARCH
  • 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 6161045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:25:01+00:00 2026-05-23T21:25:01+00:00

I have a DataTemplate for ListBoxItem that (should be) fairly simple, and just access

  • 0

I have a DataTemplate for ListBoxItem that (should be) fairly simple, and just access via {Binding} the properties of my class.

Below is my class and code that creates a simple (dummy) ItemsSource of the ListBox.

public class ChatMessage
{
    public string Message = "Testing Message";
    public DateTime DateReceived = new DateTime(2011, 07, 16, 14, 00, 05);

    public override string ToString()
    {
        return Message;
    }
}

// ....

// Dummy Data
ObservableCollection<ChatMessage> chatItems = new ObservableCollection<ChatMessage>();
for (int i = 0; i < 20; i++)
    chatItems.Add(new ChatMessage());
lbMessages.ItemsSource = chatItems;

Here’s my DataTemplate..

<DataTemplate x:Key="ChatItemListBox">
    <Grid Width="362">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="26" />
        </Grid.RowDefinitions>
        <TextBlock Text="{Binding Message}" />
        <TextBlock Grid.Row="1"Text="{Binding DateReceived}"/>
    </Grid>
</DataTemplate>

For some reason both TextBlocks are completely empty. However, if I change for example {Binding Message} to just {Binding} it will call ToString and show the message.

I know I must be missing something very simple..

Just in case it matters, the ListBox is defined as below:

<ListBox x:Name="lbMessages" Margin="0,8,0,72" ItemTemplate="{StaticResource ChatItemListBox}"/>
  • 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-23T21:25:02+00:00Added an answer on May 23, 2026 at 9:25 pm

    You should use properties, not fields.

    public class ChatMessage
    {
        public string Message { get; set; }
        public DateTime DateReceived { get; set; }
    
        public override string ToString()
        {
            return Message;
        }
    
        public ChatMessage()
        {
            Message = "Testing Message";
            DateReceived = new DateTime(2011, 07, 16, 14, 00, 05);
        }
    }
    

    This is because binding is using reflection for non-DependencyProperties, and doesn’t look for fields.

    (This advice would solve the problem for WPF, but Silverlight may have some additional quirks.)

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

Sidebar

Related Questions

I have a usercontrol that I want to use as a ListBoxItem. <ListBox.ItemTemplate> <DataTemplate>
I have a DataTemplate that will be a templated ListBoxItem, this DataTemplate has a
I have a DataTemplate within my Window's Resources section that creates a TextBlock with
I have a DataTemplate that is used by a listbox: <local:BooleanToFontColorConverter x:Key=boolToFontColor /> <DataTemplate
I have ItemsControl with DataTemplate that looks like this: I want to achieve effect
I have the following DataTemplate : <DataTemplate x:Key=ColoringLabels> <TextBlock Padding=0 Margin=0 Name=Username Text={Binding Username}
I have a ListBox with a DataTemplate that looks like this: <ListBox Name=listBox> <ListBox.ItemTemplate>
Possible Duplicate: Datatemplate inheritance I have several data types that are not subclasses, nor
I have a ListBox, that's bound to an ObservableCollection . Each ListBoxItem is displayed
I have added a DataTemplate to a ListBox class to bind my collection to:

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.