2013-06-01 150 views
0

我是一个完整的C#新手,请原谅我的无知。将DateTime和Boolean转换为字符串

我想解析字符串值到视图模型。我很难将数据库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)); 
     } 
    } 
} 
+1

说一个简单的'的ToString()'后那些特性帮忙? –

回答

1

你应该这样做

Items.Add(new ItemViewModel 
{ 

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

我们的答案对您有帮助吗? –

+0

如果我这样做,没有字符串显示,这是否意味着在ItemViewModel中有问题。 – Anth

+0

请添加更多关于你viewmodel的信息。 –

1

使用ToString();

或转换为字符串