Search code examples
c#xamldatetimexamarin.forms

DateTime.Now in XAML without binding


Can I put the date of today in a label without binding it in XAML, something like

<Label Text="DateTime.Now, StringFormat='{0:MMMM dd, yyyy}'"/>

Solution

  • Can I put the date of today in a label without binding it in XAML

    No, you can't.

    For Binding, use

    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    

    with

    <Label Content="{Binding Source={x:Static sys:DateTime.Now}, StringFormat='{0:MMMM dd, yyyy}'}" />
    

    Though, you can set it from Code behind like myLabelControl.Content = DateTime.Now;, but I would totally avoid this.