【问题标题】:How to customize GridViewColumn display for properties of class?如何为类的属性自定义 GridViewColumn 显示?
【发布时间】:2020-06-07 15:23:24
【问题描述】:

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

【问题讨论】:

标签: c# overriding tostring


【解决方案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"; 
        }
    }
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 2014-06-16
      相关资源
      最近更新 更多