Actual Date and Time bind to a Label
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
Réponses
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 Snippetpublic 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
Toutes les réponses
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?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?
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 Snippetpublic 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
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

