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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:54:23+00:00 2026-06-18T07:54:23+00:00

This is a follow up based on the answer of a previous question. I’ve

  • 0

This is a follow up based on the answer of a previous question. I’ve managed to come up with a DependencyProperty which will be updated using a Timer to always have the latest date time, and a textblock which shows the datetime. Since it’s a DependencyProperty, whenever the timer updates the value, the textblock will show the latest DateTime as well.

The dependency object

    public class TestDependency : DependencyObject
    {
        public static readonly DependencyProperty TestDateTimeProperty =
            DependencyProperty.Register("TestDateTime", typeof(DateTime), typeof(TestDependency),
            new PropertyMetadata(DateTime.Now));

        DispatcherTimer timer;

        public TestDependency()
        {
            timer = new DispatcherTimer(new TimeSpan(0,0,1), DispatcherPriority.DataBind, new EventHandler(Callback), Application.Current.Dispatcher);
            timer.Start();

        }

        public DateTime TestDateTime
        {
            get { return (DateTime)GetValue(TestDateTimeProperty); }
            set { SetValue(TestDateTimeProperty, value); }
        }

        private void Callback(object ignore, EventArgs ex)
        {
            TestDateTime = DateTime.Now;
        }

    }

The Window Xaml

    <Window.DataContext>
        <local:TestDependency/>
    </Window.DataContext>
    <Grid>
        <TextBlock Text="{Binding TestDateTime}" />
    </Grid>

This works very well, but i’m wondering, what do I have to do if I wanna format the time string in a different way, is there a way to call a ToString(formatter) on the date time before displaying it in the textblock, while maintaining the ability to auto update the textblock using the DependencyProperty? What is the correct way to do this in code behind, and the correct way to do this in Xaml, if it’s even possible?

And, if I have Multiple textboxes to show, each with a different date time format, what is the proper way to make use of only 1 timer to display all the different date time formats in different textboxes, do I have to create a DependencyProperty for each single format?

  • 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-06-18T07:54:23+00:00Added an answer on June 18, 2026 at 7:54 am

    You can use string format for this:

    <Window.DataContext>
        <wpfGridMisc:TestDependency/>
    </Window.DataContext>
    <StackPanel>
        <TextBlock Text="{Binding TestDateTime, StringFormat=HH:mm:ss}"/>
        <TextBlock Text="{Binding TestDateTime, StringFormat=MM/dd/yyyy}"/>
        <TextBlock Text="{Binding TestDateTime, StringFormat=MM/dd/yyyy hh:mm tt}"/>
        <TextBlock Text="{Binding TestDateTime, StringFormat=hh:mm tt}"/>
        <TextBlock Text="{Binding TestDateTime, StringFormat=hh:mm}"/>
        <TextBlock Text="{Binding TestDateTime, StringFormat=hh:mm:ss}"/>
    </StackPanel>
    

    Also i think you should use SetCurrentValue() when updating the DependencyProperty, you can read why here

    private void Callback(object ignore, EventArgs ex)
    {
        SetCurrentValue(TestDateTimeProperty, DateTime.Now);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-up to my previous question on Z3's Model-based Quantifier Instantiation (MBQI)
This is a follow-on question to C++0x rvalue references and temporaries In the previous
This is a follow up question to that at Can I create a HTML
I asked this question (below) a week ago and got the answer I was
Earlier today, I asked this question and got the answer I was looking for.
This question seems like it should be so simple to answer, but after days
This is a follow up question from R: t-test over all columns Suppose I
This is a follow up to this post . I refined my requirement based
Hey guys. This is a follow-on from this question : After getting the right
I'm hoping this will be a simple answer for one of you. We've got

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.