开发者

Date formatting using data annotations for a dataform in Silverlight

开发者 https://www.devze.com 2022-12-25 14:34 出处:网络
This is probably got a simple answer to it, but I am having problems just formatting the date for a dataform field..

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}.


This was solved by adding a converter to the data binding:

<df:DataField>
    <TextBlock Text="{Binding DateCreated, Converter={StaticResource DateConverter}}"/>
</df:DataField>

The DateConverter is just a class than implements the IValueConverter interface. Example below shows the date time format I needed..

/// <summary>
/// Date time formatter - for short dates for data bound items
/// </summary>
public class DateConverter : IValueConverter
{
    #region Public Methods

    #region IValueConvertor Members

    /// <summary>
    /// Convert data item to a short date
    /// </summary>
    /// <param name="value">Value</param>
    /// <param name="targetType">Target type</param>
    /// <param name="parameter">Paramter</param>
    /// <param name="culture">Culture</param>
    /// <returns>Converted object</returns>
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var date = (DateTime)value;
        return (date.ToString("dd/MM/yyyy HH:mm:ss"));
    }

    /// <summary>
    /// Convert back data item to a short date
    /// </summary>
    /// <param name="value">Value</param>
    /// <param name="targetType">Target type</param>
    /// <param name="parameter">Paramter</param>
    /// <param name="culture">Culture</param>
    /// <returns>Converted object</returns>
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var s = (string)value;
        return (DateTime.Parse(s));
    }

    #endregion IValueConvertor Members

    #endregion Public Methods
}

This works like a treat - could be improved with some null checking! :)

0

精彩评论

暂无评论...
验证码 换一张
取 消