2

數據形這可能是有一個簡單的答案,但我有隻格式化數據形場的日期問題..日期格式使用數據註解在Silverlight

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

我已綁定這

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

不管我設置dataformatstring它回來爲:具有現場dateCreated會批註筆記類如2010/4/6上午10時02分15秒

我想這格式化爲yyyy-MM-dd HH:mm:ss

我試過上面的自定義格式{0:yyyy-MM-dd hh:mm:ss},但它仍然是相同的輸出。 {0:u}或{0:s}也是如此。

回答

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 
} 

這就像一種享受 - 可以用一些空的檢查加以改進! :)