• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
.NET Framework Developer Center
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > Actual Date and Time bind to a Label
Ask a questionAsk a question
Search Forums:
  • Search Windows Presentation Foundation (WPF) Forum Search Windows Presentation Foundation (WPF) Forum
  • Search All .NET Development Forums Search All .NET Development Forums
  • Search All MSDN Forums Search All MSDN Forums
 

AnswerActual Date and Time bind to a Label

  • Wednesday, June 20, 2007 7:59 AMChristian Feininger Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    Hello,

     

    I'd like to bind the actual date and time to a Label. I tried the following code, but it doesn't work. Does anyone now why?

     

    Code Snippet

    <Window ...

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:src="clr-namespace:System;assembly=mscorlib">

    <Window.Resources>

    <ObjectDataProvider x:Key="date" ObjectType="{x:Type src:DateTime}" MethodName="Now"/>

    </Window.Resources>

     

    ....

    <Label Content="{Binding Source={StaticResource date}, Mode=OneWay}"/>

     

    Why is it not working? Did I miss something or is my idea totally wrong?

     

    Thanks

     

    Chris

    • ReplyReply
    • QuoteQuote
     

Answers

  • Wednesday, June 20, 2007 8:50 AMThomas Claudius Huber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Vote As Helpful
    0

    Hi Chris,

     

    the problem is that DateTime.Now has no mechanism for change-notification.

    So you need a custom solution.

     

    I would create an extra class that has only a now property and implements INotifyPropertyChanged. It could look like this:

     

    Code Snippet

    public class NotifyingDateTime:INotifyPropertyChanged

    {

    public event PropertyChangedEventHandler PropertyChanged;

    private DateTime _now;

    public NotifyingDateTime()

    {

    _now = DateTime.Now;

    DispatcherTimer timer = new DispatcherTimer();

    timer.Interval = TimeSpan.FromMilliseconds(100);

    timer.Tick += new EventHandler(timer_Tick);

    timer.Start();

    }

    public DateTime Now

    {

    get{return _now;}

    private set

    {

    _now = value;

    if(PropertyChanged!=null)

    PropertyChanged(this,new PropertyChangedEventArgs("Now"));

    }

    }

    void timer_Tick(object sender, EventArgs e)

    {

    Now = DateTime.Now;

    }

    }

     

    In your XAML-File you could then just bind to the Now-Property of this class and will get always the current date and time.

    If you need to show a different TimeFormat, you could use a ValueConverter to do that:

     

    Code Snippet

    <Window x:Class="MyApp.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="clr-namespace:MyApp"

    Title="MyApp" Height="300" Width="300"

    >

    <Window.Resources>

    <local:NotifyingDateTime x:Key="notifyingDate"/>

    </Window.Resources>

    <Label Content="{Binding Source={StaticResource notifyingDate},Path=Now}"/>

    </Window>

     

     

    Thomas

    • ReplyReply
    • QuoteQuote
     

All Replies

  • Wednesday, June 20, 2007 8:15 AMThomas Claudius Huber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    Hi Chris,

     

    Now isn't a method, it's a property:

     

    Code Snippet

    <Window.Resources>

    <ObjectDataProvider x:Key="date" ObjectType="{x:Type src:DateTime}"/>

    </Window.Resources>

    <Label Content="{Binding Source={StaticResource date},Path=Now,Mode=OneWay}"/>

     

    But if you do it this way, the date is just initialized and never updated. Is this that what you want?
    • ReplyReply
    • QuoteQuote
     
  • Wednesday, June 20, 2007 8:30 AMChristian Feininger Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    No,

     

    i want that it will be updated all the time automatically. I thought I can do this with databinding. Isn't that right?

    How should I do it instead?

     

     

    • ReplyReply
    • QuoteQuote
     
  • Wednesday, June 20, 2007 9:26 AMChristian Feininger Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

     Hello Thomas,

     

    yeah it works fine. Thanks. I just needed to change something a bit. Just if someone wants to use it later as well:

     

    Code Snippet

    <Window.Resources>

    <ObjectDataProvider x:Key="notifyingDate" ObjectType="{x:Type local:NotifyingDateTime}"/>

    </Window.Resources>

     

    Thanks a lot.

     

    Cheers

     

    Chrsi

    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement