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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:12:05+00:00 2026-05-13T23:12:05+00:00

I am coming from Flex where you can do just about anything inside of

  • 0

I am coming from Flex where you can do just about anything inside of curly braces. I am trying to get a TextBlock to display today’s Date and Time without just coding it in C#. I have tried many different variations of the following with no luck.

TextBlock Text="{Source=Date, Path=Now, StringFormat='dd/MM/yyyy'}"

I know I could probably just set a property MyDate and bind to that but why can’t I bind directly to the DateTime.Now property?

  • 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-13T23:12:05+00:00Added an answer on May 13, 2026 at 11:12 pm

    Binding in Silverlight requires a Source object or a Dependency object. From that source object you can bind to Properties (hence by definition you are binding to instance members) or Dependency Properties.

    Since DateTime.Now is a static property you cannot bind to it in Silverlight directly, hence some code is needed. The next best thing is to use code to:-

    • ensure as much of what you need can be expressed in XAML
    • to do so in an as de-coupled manner as possible.

    Hence we can analyse that we need two things.

    1. Expose the static members of DateTime as instance properties of some object
    2. Have some way to format the DateTime to a desirable output.

    To handle the first item I would create a StaticSurrogate class, where I would create instance properties for the static properties that we need access to:-

    public class StaticSurrogate
    {
        public DateTime Today { get { return DateTime.Today; } }
        public DateTime Now { get { return DateTime.Now; } }
    }
    

    Now we need a way to format a Date time. A value converter is the right tool for this job, borrowing heavily from this Tim Heuer Blog :-

    public class FormatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (parameter != null)
            {
                string formatterString = parameter.ToString();
    
                if (!String.IsNullOrEmpty(formatterString))
                {
                    return String.Format(culture, String.Format("{{0:{0}}}", formatterString), value);
                }
            }
    
            return (value ?? "").ToString();
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    With these two classes in place we can now do the rest in Xaml, first we need instances of these classes in our resources:-

    <UserControl.Resources>
        <local:StaticSurrogate x:Key="Static" />
        <local:FormatConverter x:Key="Formatter" />     
    </UserControl.Resources>
    

    Now we can wire up the TextBlock :-

    <TextBlock Text="{Binding Today, Source={StaticResource Static},
        Converter={StaticResource Formatter}, ConverterParameter='dd MMM yyy'}" />
    

    Note that this approach has the following advantages:-

    • we do not need to add code to the UserControl on which the TextBlock is placed, nor do we have to fiddle around with any data context.
    • The Static resources could be placed in the App.Resources which would make the creation of the TextBlock entirely independent of having to add anything else to the UserControl.
    • The formatting used to display the date can be independently modified.
    • Access to additional static properties can easily be added to the StaticSurrogate class.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im just starting to get to grips with silverlight 3, coming from ASP.NET and
I'm a newbie doing Objective-C, coming from Flex/Actionscript development. I have an iPhone app
I am trying to bind an ArrayCollection result coming in from the server to
Hi I am trying to send a simple HTTP message from Flex to C#
I'm coming from a flash/flex background, so forgive me if this is an off
We are working on a flex project I need to display a number coming
coming from .Net world, just want to know what is the corresponding collection to
Coming from a Flex-Flash IDE, I was able to set breakpoints in my code
Coming from VB, JavaScript isn't very easy to get the hang of. Please don't
Coming from a c++ background I'm curious about object assignment in Ruby. What considerations

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.