【发布时间】:2020-06-07 15:23:24
【问题描述】:
我有一个具有多个属性的类文章。
我想知道是否可以覆盖 bool 和 DateTime 属性的 ToString 方法,以便将布尔值打印为“是/否”,将日期时间打印为自定义文本。
想法是,当这些属性打印在 ListView 中,GridViewColumn 绑定到每个属性时,它们不会打印标准的 ToString 值。
public class Article
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Content { get; set; }
public int NumberOfWords { get; set; }
public string Category { get; set; }
public bool CanBePublished { get; set; }
public bool Published { get; set; }
public int Length { get; set; }
public DateTime CreationDate { get; set; }
public Article() { }
public Article(string title, string author, string content, int numberOfWords, string category, bool canBePublished, int length)
{
Title = title;
Author = author;
Content = content;
NumberOfWords = numberOfWords;
Category = category;
CanBePublished = canBePublished;
Length = length;
Published = false;
CreationDate = DateTime.Now;
}
}
【问题讨论】:
-
您可以创建将 DateTime 存储在字符串中的私有变量和公共 getter 以在该 ToString 覆盖中检索该字符串
-
这能回答你的问题吗? WPF: How do I apply custom formatting to a ListView? 快速谷歌搜索表明这也适用于 gridview
标签: c# overriding tostring