如何自定义类的属性的GridViewColumn显示?

问题描述 投票:0回答:2

我有一个有多个属性的类Article.我想知道是否可以覆盖ToString方法来显示类中的 boolDateTime 属性,使布尔值打印为 "YesNo",DateTime打印为自定义文本。

我们的想法是,当这些属性被打印在 ListViewGridViewColumn 绑定到每个属性,他们不打印标准的 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;
    }
}
c# overriding tostring
2个回答
1
投票

你可以定义get方法从这些字段中获取格式化的值,如下所示。创建一个视图模型类,并通过定义一个get方法来实现,并使用该属性来读取数据。如 Article_ViewModelObject.CreationDateVal

public class Article_ViewModel: Article
{
     public string CreationDateVal  
    {
        get
        {
            return  CreationDate.ToString(); 
        }
    }
     public string CanBePublishedVal  
    {
        get
        {
            return CanBePublished ? "Yes" : "No"; 
        }
    }
}

0
投票

修改你的模型数据类来改变你在视图中看到的东西不是一个好主意。大多数显示模型数据对象的平台都有一种基于原始数据修改你所看到的东西的方法。

根据你所使用的堆栈,搜索基于原始数据修改视图来代替。如果你能展示你的UI代码,我们可以提供进一步的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.