2017-05-03 41 views
0

我試圖以編程方式將列表中的數據綁定到WPF中的GridView。 它完美的工作,但我想格式化一個TimeSpan列以滿足我的需求,但是當我使用任何StringFormat時,該列只保留爲空。C#WPF數據網格格式時間範圍

我有以下(簡化)代碼:

foreach (var song in _manager.GetLibrarySongs()) 
{ 
    LibraryView.Items.Add(song); 
} 

DataGridTextColumn durationColumn = new DataGridTextColumn 
{ 
    Header = "Duration", 
    Binding = new Binding("Duration") { StringFormat = "" } 
}; 
LibraryView.Columns.Add(durationColumn); 

的StringFormat爲空,在這裏,但無論我擺在那裏,它使立柱稍稍空。我試過「hh \:mm \:ss」,「hh \:mm \\:ss」,「{} {0:g}」,...

有誰知道我在做什麼錯誤? 在此先感謝!

+0

僅僅因爲我在答案中很有趣。爲什麼不在xaml中做? –

+0

@MightyBadaboom我打算讓用戶選擇顯示哪些列,但我不確定是否我最終會這樣做,或者只是綁定xaml中的所有列,然後隱藏列。我只是想這樣玩弄它。 – JC97

回答

1

以下代碼已在Visual Studio 2015中使用,並且具有帶定義格式的DateTime和TimeSpan列的DataGrid。

對於日期以下字符串用於 「YYYY-MM-DD」 而對於時間字符串 「毫米\:ss」 的使用(注意雙反斜線)

public partial class MainWindow : Window { 
    public MainWindow() { 
     InitializeComponent(); 

     dataGrid = new DataGrid(); 

     var stackPanel = new StackPanel { Orientation = Orientation.Vertical }; 
     stackPanel.Children.Add(dataGrid); 

     Content = stackPanel; 

     Rnd = new Random(); 
     Now = DateTime.Now; 
     Counter = 1; 

     foreach (var song in GetSongs()) 
      dataGrid.Items.Add(song); 

     // var timeSpanConverter = new TimeSpanConverter(); 
     var titleColumn = new DataGridTextColumn { Header = "Title", Binding = new Binding("Title") }; 
     var authorColumn = new DataGridTextColumn { Header = "Author", Binding = new Binding("Author") }; 
     var albumColumn = new DataGridTextColumn { Header = "Album", Binding = new Binding("Album")}; 
     // var durationColumn = new DataGridTextColumn {Header = "Duration", Binding = new Binding("Duration") { Converter = timeSpanConverter } }; 
     var durationColumn = new DataGridTextColumn { Header = "Duration", Binding = new Binding("Duration") { StringFormat = "mm\\:ss" } }; 
     var releaseColumn = new DataGridTextColumn { Header = "Release", Binding = new Binding("Release") { StringFormat = "yyyy-MM-dd" } }; 

     dataGrid.Columns.Add(titleColumn); 
     dataGrid.Columns.Add(authorColumn); 
     dataGrid.Columns.Add(albumColumn); 
     dataGrid.Columns.Add(durationColumn); 
     dataGrid.Columns.Add(releaseColumn); 
    } 

    private DataGrid dataGrid; 

    private int? Counter; 
    private DateTime? Now; 
    private Random Rnd; 

    private TimeSpan GetDuration() { return TimeSpan.FromSeconds(30 + Rnd.Next(500)); } 
    private DateTime GetRelease() { Counter += 1; return Now.Value.AddMilliseconds(Counter.Value); } 
    private string GetTitle() { return $"Title {Counter}"; } 
    private string GetAlbum() { return $"Album {Counter}"; } 
    private string GetAuthor() { return $"Author {Counter}"; } 

    private IList<Song> GetSongs() 
    { 
     var list = new List<Song>(); 

     for(var i = 0; i < 1000; i++) 
      list.Add(new Song() { 
       Title = GetTitle(), 
       Album = GetAlbum(), 
       Author = GetAuthor(), 
       Release = GetRelease(), 
       Duration = GetDuration() 
      }); 
     return list; 
    } 
} 

public class Song { 
    public string Title { get; set; } 
    public string Author { get; set; } 
    public string Album { get; set; } 
    public DateTime Release { get; set; } 
    public TimeSpan Duration { get; set; } 
} 

public class TimeSpanConverter : IValueConverter { 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { 
     return ((TimeSpan)value).ToString("mm\\:ss"); 
    } 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { 
     throw new NotImplementedException(); 
    } 
} 

在情況下,如果用的StringFormat方法是不工作(不應該是這種情況),你可以嘗試與轉換器的方法(評論代碼爲持續時間欄)