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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:59:30+00:00 2026-05-21T11:59:30+00:00

I am working a very simple lookless control, and I can’t seem to get

  • 0

I am working a very simple lookless control, and I can’t seem to get one of the template bindings to work. In the control I have two Dependency Properties, the one that is a string works, and the one that is an int does not.

The csharp code looks like this:

using System;
using System.Windows;
using System.Windows.Controls;

namespace ControlDemo
{
    public class TextControlLookless : Control
    {
        #region Title

        public static readonly DependencyProperty ChartTitleProperty =
            DependencyProperty.Register("ChartTitle", typeof(string), typeof(TextControlLookless),
            null);


        public String ChartTitle
        {
            get { return (string)GetValue(ChartTitleProperty); }
            set
            {
                SetValue(ChartTitleProperty, value);
            }
        }

        #endregion

        #region Value

        public static readonly DependencyProperty ChartValueProperty =
            DependencyProperty.Register("ChartValue", typeof(int), typeof(TextControlLookless),
            null);


        public int ChartValue
        {
            get { return (int)GetValue(ChartValueProperty); }
            set
            {
                SetValue(ChartValueProperty, value);
            }
        }

        #endregion

        #region ctor

        public TextControlLookless()
        {
            this.DefaultStyleKey = typeof(TextControlLookless);
        }

        #endregion

    }
}

And the xaml for the control looks like this:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ControlDemo">

<Style TargetType="local:TextControlLookless">
    <Setter Property="ChartTitle" Value="Set Title" />
    <Setter Property="ChartValue" Value="1" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:TextControlLookless">
                <Grid x:Name="Root">
                    <Border BorderBrush="Black" BorderThickness="2">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <TextBlock Text="{TemplateBinding ChartTitle}" />
                            <TextBlock Text="{TemplateBinding ChartValue}" Grid.Row="1" />
                        </Grid>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

When I put this on a page, I can see the ChartTitle (either Set Title, or whatever I set it to), but the ChartValue never shows up. If I change its type to a string, it does show up, so I must be missing something.

  • 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-21T11:59:30+00:00Added an answer on May 21, 2026 at 11:59 am

    The problem is that TemplateBinding is a far more primitive operation than Binding. Binding is an actual class and includes some helpful features including the implicit conversion of strings back and forth between other data types.

    TemplateBinding is purely a markup instruction and crucially in your case does not do type conversion for you. Hence the dependency property being bound to a Text property of a TextBlock must be a string.

    You have two choices:-

    One choice is instead using TemplateBinding give the TextBlock a name and assign its Text in the ChartValue property changed call back:-

        #region Value
    
        public static readonly DependencyProperty ChartValueProperty =
            DependencyProperty.Register("ChartValue", typeof(int), typeof(TextControlLookless),
            new PropertyMetadata(0, OnChartValuePropertyChanged));
    
        private static void OnChartValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TextControlLookless source = d as TextControlLookless;
            source.Refresh();
        }
    
    
        public int ChartValue
        {
            get { return (int)GetValue(ChartValueProperty); }
            set
            {
                SetValue(ChartValueProperty, value);
            }
        }
    
        #endregion
    
        private TextBlock txtChartValue { get; set; }
    
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            txtChartValue = GetTemplateChild("txtChartValue") as TextBlock;
            Refresh();
        }
    
        private void Refresh()
        {
            if (txtChartValue != null)
            {
                txtChartValue.Text = ChartValue.ToString();
            }
        }
    

    where the xaml looks like:-

        <TextBlock x:Name="txtChartValue" Grid.Row="1" />
    

    The other choice is to create a private dependency property for the value with type of string:-

            #region Value
    
            public static readonly DependencyProperty ChartValueProperty =
                DependencyProperty.Register("ChartValue", typeof(int), typeof(TextControlLookless),
                new PropertyMetadata(0, OnChartValuePropertyChanged));
    
            private static void OnChartValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                d.SetValue(ChartValueStrProperty, e.NewValue.ToString());
            }
    
            private static readonly DependencyProperty ChartValueStrProperty =
                DependencyProperty.Register("ChartValueStr", typeof(string), typeof(TextControlLookless),
                new PropertyMetadata("0"));
    
            public int ChartValue
            {
                get { return (int)GetValue(ChartValueProperty); }
                set
                {
                    SetValue(ChartValueProperty, value);
                }
            }
    
            #endregion
    

    where the xaml looks like:-

            <TextBlock Text="{TemplateBinding ChartValueStr}" Grid.Row="1" />
    

    Note that the ChartValueStrProperty is private and I haven’t bothered creating a standard .NET property to cover it. TemplateBinding actually takes the property name you assign suffixes is with “Property” then looks for a static field on the target type.

    Both approaches have their strengths and weaknesses. The first approach is the more common pattern but takes a little more code and is less flexiable (the control displaying the value must be a TextBlock). The second is more flexiable and uses less code but is somewhat unorthodox.

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

Sidebar

Related Questions

hey guys, i'm working on a very simple breadcrumb-path solution, however i have one
This might be a very simple question, but I can't get it working. All
I'm working on a very simple iPhone app, that in the end will have
I'm working on SQL server 2005 and I have a very simple stored procedure:
I have a very simple code, which annoyingly was working and for the life
I have developed a simple location aware iPhone application which is functionally working very
Its very simple but somehow not working (weird!!). I have a List of a
I am working on a very simple application, using MVC2 Preview 1. I have
Actually I'm working on very simple FTP server. Now I have problem with sending
So I have a very simple app I am working on. It's purpose is

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.