将日期时间和布尔值转换为字符串

本文关键字:转换 字符串 布尔值 日期 时间 | 更新日期: 2023-09-27 18:16:16

我是个彻头彻尾的c#新手,请原谅我的无知。

我试图将字符串值解析为视图模型。我很难将数据库DateTime和布尔值转换为字符串,作为LineOne, LineTwo和LineThree属性的一部分。我该怎么做呢?

private void mapChecks() 
{
    bool FoundResult = false;
    // Check if object is loaded
    if (Items.Count == 0)
    {
    //Add everything
        foreach (xtn_UnresolvedCheck check in MyChecks)
        {
                Items.Add(new ItemViewModel
                    {
                        LineOne = check.ClientName,
                        LineTwo = check.NSMDateTime,
                        LineThree = check.HaveRead,
                        MyappId = check.MonitoringID
          }
        );
    }
}

ItemViewModel:

namespace App
{
    public class ItemViewModel : INotifyPropertyChanged
    {
        private int _myappId;
        public int MyappId
        {
            get
            {
                return _myappId;
            }
            set
            {
                if (value != _myappId)
                {
                    _myappId = value;
                    NotifyPropertyChanged("MyappId");
                }
            }
        }
    private bool _isFavorite;
    public bool IsFavorite
    {
        get
        {
            return _isFavorite;
        }
        set
        {
            if (value != _isFavorite)
            {
                _isFavorite = value;
                NotifyPropertyChanged("IsFavorite");
            }
        }
    }
    private string _lineOne;
    public string LineOne
    {
        get
        {
            return _lineOne;
        }
        set
        {
            if (value != _lineOne)
            {
                _lineOne = value;
                NotifyPropertyChanged("LineOne");
            }
        }
    }
    private string _lineTwo;
    public string LineTwo
    {
        get
        {
            return _lineTwo;
        }
        set
        {
            if (value != _lineTwo)
            {
                _lineTwo = value;
                NotifyPropertyChanged("LineTwo");
            }
        }
    }
    private string _lineThree;
    public string LineThree
    {
        get
        {
            return _lineThree;
        }
        set
        {
            if (value != _lineThree)
            {
                _lineThree = value;
                NotifyPropertyChanged("LineThree");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

将日期时间和布尔值转换为字符串

你应该这么做

 Items.Add( new ItemViewModel
 {
        LineOne = check.ClientName,
        LineTwo = check.NSMDateTime.ToString(),
        LineThree = check.HaveRead.ToString(),
        MyappId = check.MonitoringID
 });

使用ToString();

或强制转换为string