2013-06-01 151 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();

或轉換爲字符串