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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:25:16+00:00 2026-05-16T15:25:16+00:00

In the Xaml below, the first control (the TextBlock by itself) has no problem

  • 0

In the Xaml below, the first control (the TextBlock by itself) has no problem binding and rendering the value of RecordCount. But in the second control (the Label with the ContentTemplate), the value of RecordCount is not rendered. However, the literal “Cars” is rendered fine. So I know the ContentTemplate is working, but the binding to RecordCount from within the ContentTemplate is not. What am I doing wrong?

<TextBlock Text="{Binding RecordCount}"/>

<Label HorizontalAlignment="Center" >
     <Label.ContentTemplate>
          <DataTemplate>
               <StackPanel Orientation="Horizontal" Width="100">
                    <TextBlock Text="{Binding RecordCount}"/>
                    <TextBlock Text=" Cars"/>
               </StackPanel>
          </DataTemplate>
     </Label.ContentTemplate>
</Label>
  • 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-16T15:25:17+00:00Added an answer on May 16, 2026 at 3:25 pm

    Set the Content property on the Label to the current DataContext:

    <Label HorizontalAlignment="Center" Content="{Binding}">
    

    or, set the StackPanel as the Content and don’t use a template at all:

    <Label HorizontalAlignment="Center">
        <StackPanel Orientation="Horizontal" Width="100">
            <TextBlock Text="{Binding RecordCount}"/>
            <TextBlock Text=" Cars"/>
        </StackPanel>
    </Label>
    

    The ContentTemplate is used to present the Content. Since it is null, the DataContext is null when your template is instantiated. The TextBlocks are still created, so Cars is rendered, but null doesn’t have a RecordCount property so the first text block is rendered with no text.

    Also, if you are only using two TextBlocks to concatenate text, you can use the StringFormat property in .NET 3.5 SP1 or later:

    <Label Content="{Binding RecordCount}" ContentStringFormat="{}{0} Cars"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple custom window (XAML below). When first loaded, it will add
With the below binding in XAML, the selected item is not displayed in a
I'm trying to create a custom control with C#/WPF. See below for the XAML
I have below XAML code : <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml DataContext={Binding RelativeSource={RelativeSource Self}} WindowStartupLocation=CenterScreen
The labels in the example below (WPF/XAML) just parade off the screen, no wrapping
Here's the XAML structure. You'll see below that I'm subscribing to the Loaded event
I am learning backgroundworker class in WPF. The code below is in file MainWindow.xaml.cs
xaml side: <DataGrid Name=MyGrid ItemsSource={Binding} /> I tried also with: <DataGrid Name=MyGrid ItemsSource={Binding MyObj}
The xaml for the first style works as I want, producing a button with
Windows Phone 7.5 app. Designer has created Home.xaml in Blend that has 3 images(menu

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.