2

这可能是一个简单的答案,但我在格式化数据表单字段的日期时遇到问题..

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

我已将此绑定到一个注释类,该类具有字段 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; }

无论我设置什么数据格式字符串,它都会返回:例如 4/6/2010 10:02:15 AM

我希望这个格式为 yyyy-MM-dd HH:mm:ss

我已经尝试了 {0:yyyy-MM-dd hh:mm:ss} 上面的自定义格式,但它仍然是相同的输出。{0:u} 或 {0:s} 也是如此。

4

1 回答 1

4

这是通过向数据绑定添加转换器来解决的:

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

DateConverter 只是一个实现 IValueConverter 接口的类。下面的示例显示了我需要的日期时间格式..

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

这就像一种享受 - 可以通过一些空检查来改进!:)

于 2010-04-13T09:32:35.637 回答