C#WPF数据网格格式时间跨度

时间:2017-05-03 10:17:29

标签: c# wpf data-binding formatting wpfdatagrid

我尝试以编程方式将列表中的数据绑定到WPF中的GridView。 它工作得很好,但我想根据我的需要格式化TimeSpan列,但是当我使用任何StringFormat时,该列只保持空白。

我有以下(简化)代码:

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}&#34 ; ,...

有谁知道我做错了什么? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

以下代码已在Visual Studio 2015中使用,并且DataGrid具有定义格式的DateTime和TimeSpan列。

对于使用字符串的日期" yyyy-MM-dd" 而对于时间字符串" mm \: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的方法不起作用(不应该是这种情况),您可以尝试使用Converter(在“持续时间”列的代码中注释)