Answered by:
Show current datetime in TextBlock using only xaml

Question
-
I want to show the current date/time in a TextBlock using only xaml binding. I know I can do this with one line of code, but just want to know if it can be done easily in xaml.
I tried using the following snippets:
xmlns:System="clr-namespace:System;assembly=mscorlib" <UserControl.Resources> <System:DateTime x:Key="DateTimeDataSource" d:IsDataSource="True" /> </UserControl.Resources> <TextBlock x:Name="DateTimeText" FontSize="14" FontWeight="Bold" DataContext="{Binding Source={StaticResource DateTimeDataSource}}" Text="{Binding Now, Mode=OneWay, StringFormat=\{0:d\}}" Margin="5" />
This even displays correctly in the Blend designer. At runtime though, I get this:
XamlParseException
Unknown element: DateTime. [Line: 16 Position: 71](Line 16 is the line with the resource declared.)
Any ideas?
Tuesday, January 12, 2010 1:33 PM
Answers
-
DateTime is not supported in resources when mapped
from documentation
The only supported constructs from a mapped System namespace, mscorlib assembly combination in Silverlight (typically mapped as sys:) are sys:Double, sys:String, and sys:Int32. (The intention here is to use the "primitives" defined there as object elements, typically for use as a resource in a resource dictionary.)
Tuesday, January 12, 2010 3:49 PM
All replies
-
Try a ValueConverter. Here's a nice blog post that shows how.
Tuesday, January 12, 2010 2:01 PM -
That converter will come in handy I'm sure, but I still couldn't get it to work. It doesn't even get past parsing the xaml... it says it doesn't know what System:DateTime is. For now, I ended up doing this:
Private _timer = New Timer(New TimerCallback(AddressOf Timer_Tick), Nothing, 0, 1000) ' Timer for the date/time ticker in upper-right. Public Sub Timer_Tick(ByVal state As Object) Dispatcher.BeginInvoke(AddressOf TimerWork) End Sub Private Sub TimerWork() Try DateTimeText.Text = DateTime.Now.ToString("U") Catch End Try End Sub
Tuesday, January 12, 2010 2:35 PM -
it says it doesn't know what System:DateTime is.
I guess you mean System.DateTime
Tuesday, January 12, 2010 2:40 PM -
System:DateTime was where I was trying to specify the DateTime as a resource in the <UserControl.Resources>. I also tried with the converter in various ways, such as this:
<TextBlock x:Name="DateTimeText2" FontSize="14" FontWeight="Bold" Text="{Binding Source={StaticResource System.DateTime.Now}, Converter={StaticResource DateTimeConverter}, ConverterParameter=d, Mode=OneWay, StringFormat=\{0:U\}}" Margin="5" />
That way doesn't throw an error, but it also doens't put anything into the textblock, or even get into the converter code. I searched for about an hour all over, and could only find someone doing it in WPF, but it wouldn't work in SL 4. Either I'm missing something simple, or you just can't bind to System.DateTime.Now in SL xaml.
Tuesday, January 12, 2010 2:50 PM -
DateTime is not supported in resources when mapped
from documentation
The only supported constructs from a mapped System namespace, mscorlib assembly combination in Silverlight (typically mapped as sys:) are sys:Double, sys:String, and sys:Int32. (The intention here is to use the "primitives" defined there as object elements, typically for use as a resource in a resource dictionary.)
Tuesday, January 12, 2010 3:49 PM -
Thanks for that info. It would be nice if it supported something like that... oh well.
Tuesday, January 12, 2010 8:05 PM -
Try something like
<UserControl x:Class="Datetime.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:sys="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"> <UserControl.Resources> <controls:Calendar x:Key="cal" /> <TextBlock x:Key="dt" Tag="{Binding DisplayDate.Now,Source={StaticResource cal}}"/> </UserControl.Resources> <Grid x:Name="LayoutRoot"> <StackPanel> <TextBlock Text="{Binding Tag.Hour, Source={StaticResource dt} }"/> </StackPanel> </Grid> </UserControl>
Tuesday, March 16, 2010 5:08 AM -
This should work:
<UserControl x:Class="MainPage.CurrentDateTime" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" x:Name="CurrentDateTimePanel" d:DesignHeight="300" d:DesignWidth="300" > <UserControl.Resources> <System:DateTime x:Key="DateTimeDataSource" d:IsDataSource="True" /> </UserControl.Resources> <Grid> <TextBlock HorizontalAlignment="left" Margin="8" DataContext="{Binding Source={StaticResource DateTimeDataSource}}" Text="{Binding Now, Mode=OneWay, StringFormat=\{0:d\}}" > </TextBlock> </Grid> </UserControl>
Thursday, July 15, 2010 6:04 PM -
Hai !! I tried this code But when I am runing my application I am getting following error.
XMLPARSEEXECPTION Ocur... The type 'DateTime' was not found....
Please Give me the solution .. Or else can i get system Date and Time Dynamically by C# Coding in Textbox feild.
Thanks
Saturday, July 16, 2011 2:17 AM -
See the link, it is not supported you can do it as mentioned in the link below
http://stackoverflow.com/questions/2430664/current-date-in-silverlight-xaml-textblock
- Proposed as answer by BorisAdams Tuesday, August 9, 2016 12:02 AM
Saturday, July 16, 2011 2:51 AM -
I got the easy way to get Date in texbox.. Here is the code
string Date = Convert.ToString(DateTime.Now.Date);
and then just assign it to Text box feild ....
txtDate.Text = Date;
string Date = Convert.ToString(DateTime.Now.Date);txtDate.Text = Date;So no need to write XAML code to display it in Textbox........Saturday, July 16, 2011 5:29 AM -
Correct. As described in the link we cannot access a static method (ie. DateTime.Now) in Silverlight XAML and must create a wrapper class to provide as a resource to the page. It's actually quite easy, and the link describes exactly how to do it.
The same applies to WinRT (which is what brought me here).
Tuesday, August 9, 2016 12:01 AM