2

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?

Community
  • 1
  • 1
black eyed pea
  • 427
  • 1
  • 7
  • 16

2 Answers2

6

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);
}
Community
  • 1
  • 1
Johan Larsson
  • 17,112
  • 9
  • 74
  • 88
1

Binding has a StringFormat property:

<TextBlock Text="{Binding TestDateTime, StringFormat=HH:mm:ss}"/>

See also Standard Date and Time Format Strings and Custom Date and Time Format Strings.

Clemens
  • 123,504
  • 12
  • 155
  • 268