Date formatting using data annotations for a dataform in Silverlight

Posted by Aim Kai on Stack Overflow See other posts from Stack Overflow or by Aim Kai
Published on 2010-04-06T09:06:44Z Indexed on 2010/04/06 9:13 UTC
Read the original article Hit count: 999

This is probably got a simple answer to it, but I am having problems just formatting the date for a dataform field..

        <df:DataForm x:Name="Form1" ItemsSource="{Binding Mode=OneWay}" AutoGenerateFields="True"
                AutoEdit="True" AutoCommit="False"
                CommitButtonContent="Save"
                CancelButtonContent="Cancel"               
                CommandButtonsVisibility="Commit"
                LabelPosition="Top" ScrollViewer.VerticalScrollBarVisibility="Disabled"
                EditEnded="NoteForm_EditEnded"> 
        <df:DataForm.EditTemplate>
            <DataTemplate>                    
                <StackPanel>
                    <df:DataField>
                        <TextBox Text="{Binding Title, Mode=TwoWay}"/>
                    </df:DataField>

                    <df:DataField>
                        <TextBox Text="{Binding Description, Mode=TwoWay}" AcceptsReturn="True" HorizontalScrollBarVisibility="Auto"
                                 VerticalScrollBarVisibility="Auto" Height="" TextWrapping="Wrap" SizeChanged="TextBox_SizeChanged"/>                        
                    </df:DataField>

                    <df:DataField>
                        <TextBlock Text="{Binding Username}"/>
                    </df:DataField>

                    <df:DataField>
                        <TextBlock Text="{Binding DateCreated}"/>
                    </df:DataField>
                </StackPanel>
            </DataTemplate>
        </df:DataForm.EditTemplate>
    </df:DataForm>

I have bound this to a note class which has the annotation for field DateCreated:

 /// <summary>
    /// Gets or sets the date created of the noteannotation
    /// </summary>
    [Display(Name="Date Created")]
    [Editable(false)]
    [DisplayFormat(DataFormatString = "{0:u}", ApplyFormatInEditMode = true)]
    public DateTime DateCreated { get; set; }

Whatever I set the dataformatstring it comes back as: eg 4/6/2010 10:02:15 AM

I want this formatted as yyyy-MM-dd HH:mm:ss

I have tried the custom format above {0:yyyy-MM-dd hh:mm:ss} but it remains the same output. The same happens for {0:u} or {0:s}. Any help would be gratefully received. :)

© Stack Overflow or respective owner

Related posts about silverlight-3.0

Related posts about data-annotation