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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:59:56+00:00 2026-06-18T03:59:56+00:00

I want to learn how to use Dependency Objects and Properties. I have created

  • 0

I want to learn how to use Dependency Objects and Properties. I have created this class,

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

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

The window class is like this

public partial class MainWindow : Window
{
    private TestDependency td;
    public MainWindow()
    {
        InitializeComponent();
        td = new TestDependency();
        td.TestDateTime = DateTime.Now;
    }
}

Now I want to use it to show a the current DateTime in the TextBlock which updates itself every second, by adding this to a grid

<Grid>
    <TextBlock Text="{Binding TestDateTime,ElementName=td}" Width="200" Height="200"/>
</Grid>

I can see the TextBlock, but there is no Date Time value in it at all. What am I doing wrong?

  • 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-18T03:59:58+00:00Added an answer on June 18, 2026 at 3:59 am

    First of all if you want to update the display time once a second your going to need a timer to trigger an update. A DispatchTimer works works well for that.

    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;
        }
    
    }
    

    Next we need to modify the XAML so it binds properly to the updated dependency object.

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

    Since we set the DataContext in XAML you can actually delete all of the code behind code in the MainWindow constructor.

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

Sidebar

Related Questions

I want to learn how to use Struts 2 and I created a simple
I want to learn how to use MSBuild ReadLinesFromFile Task I observe Output tag
If you want to learn how to use Perl interpreter threads, there's good documentation
I made a tic-tac-toe game in c++ want to learn how to use SDL
I want to learn and use Drupal or Django for the following: dynamic web
I want to learn how to use JGoodies binding (since beans binding seems dead
I'm really new to Apache Hadoop. But I want to learn how to use
I want to learn and use SBCL because of its ease of learning and
I want to learn to use Fluent NHibernate, and I'm working in VS2010 Beta2,
I want to learn how to use regular expressions in Java and found the

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.