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

How to customize GridViewColumn display for properties of class?

我有一篇 class 文章有几个属性。 我想知道是否可以覆盖 boolDateTime 属性的 ToString 方法,以便它们打印为 "Yes/No" 的布尔值和打印为自定义文本的 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 方法以从这些字段中获取格式化值,如下所示。为此创建一个视图模型 class 并通过定义 get 方法以这种方式进行。并使用 属性 读取数据。喜欢Article_ViewModelObject.CreationDateVal

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

修改您的模型数据 class 来改变您在视图中看到的内容并不是一个好主意。大多数显示模型数据对象的平台都有一种方法可以根据原始数据修改您所看到的内容。

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