以下代碼已在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方法是不工作(不應該是這種情況),你可以嘗試與轉換器的方法(評論代碼爲持續時間欄)
僅僅因爲我在答案中很有趣。爲什麼不在xaml中做? –
@MightyBadaboom我打算讓用戶選擇顯示哪些列,但我不確定是否我最終會這樣做,或者只是綁定xaml中的所有列,然後隱藏列。我只是想這樣玩弄它。 – JC97