简体   繁体   English

将DateTime和Boolean转换为字符串

[英]Convert DateTime and Boolean to string

I'm a complete C# novice, please excuse my ignorance. 我是一个完整的C#新手,请原谅我的无知。

I'm trying to parse string values into a view-model. 我正在尝试将字符串值解析为视图模型。 I'm having difficulty converting the database DateTime and Boolean values into strings as part of the LineOne, LineTwo and LineThree properties. 我很难将数据库DateTime和布尔值转换为字符串,作为LineOne,LineTwo和LineThree属性的一部分。 How do I do this? 我该怎么做呢?

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: 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));
        }
    }
}

You should do this 你应该做这个

 Items.Add( new ItemViewModel
 {

        LineOne = check.ClientName,
        LineTwo = check.NSMDateTime.ToString(),
        LineThree = check.HaveRead.ToString(),
        MyappId = check.MonitoringID
 });

Use ToString(); 使用ToString();

or cast to string 或者施放到字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM