我有一个带有多个属性的类Article。我想知道是否有可能为bool和DateTime属性覆盖toString方法,以便对于布尔值,它们将打印为“是/否”,而DateTime将作为自定义文本。
想法是,当这些属性打印在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;
}
}
提前感谢!
您可以定义get方法以从那些字段中获取格式化的值,如下所示
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 string CreationDateVal
{
get
{
return CreationDate.ToString();
}
}
public string CanBePublishedVal
{
get
{
return CanBePublished ? "Yes" : "No";
}
}
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;
}
}
修改模型数据类以更改视图中的视图不是一个好主意。大多数显示模型数据对象的平台都可以根据原始数据来修改您所看到的内容。
取决于您所使用的堆栈,而是搜索原始数据来修改视图。如果您可以显示您的UI代码,我们将为您提供进一步的帮助。