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

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

我有一个具有多个属性的类Article。我想知道是否有可能为boolDateTime属性覆盖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;
    }
}
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.