2015-08-20 42 views
2

我做了什麼我創建了一個日曆,我的代碼位於view model中,並且能夠突出顯示創建類的日期,它繼承了IValueConverter。現在的錯誤是,當我在模擬器中更改月份並將當前日期稱爲第20日時,即使它是不同的月份,日期也會突出顯示。當我更改月份時,property會在我的view model中更新。那麼我如何才能在我的converter class中訪問此property如何訪問在類中的viewmodel中聲明的屬性?

這裏是我的代碼:

完全轉換器類:

public class DateColorConvertor : IValueConverter 
{ 

    public object ConvertBack(object value, Type targetType, object parameter, 
      System.Globalization.CultureInfo culture) 
    { 
     return new object(); 
    } 
    public object Convert(object value, Type targetType, object parameter, string language) 
    { 

     string dt = value.ToString(); 
     if (dt == (DateTime.Now.Day).ToString()) 
      return new SolidColorBrush(Colors.Blue); 
     else 
      return new SolidColorBrush(Colors.Red); 
     //throw new NotImplementedException(); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, string language) 
    { 
     throw new NotImplementedException(); 
    } 
} 

完整視圖模式:

public class calendarViewModel : ViewModelBase 
{ 
    DateTime calendarDate; 
    public calendarViewModel() 
    { 
     calendarDate = DateTime.Today; 
     Initialize_Calendar(calendarDate); 
    } 

    private ObservableCollection<string> _DATECollection = new ObservableCollection<string>(); 

    public ObservableCollection<string> DateCollection 
    { 
     get 
     { 
      return _DATECollection; 
     } 
     set 
     { 
      _DATECollection = value; 
     } 
    } 

    private ObservableCollection<Event> _eventCollection = new ObservableCollection<Event>(); 
    public ObservableCollection<Event> EventCollection 
    { 
     get 
     { 
      return _eventCollection; 
     } 
     set 
     { 
      _eventCollection = value; 
     } 
    } 

    /// <summary> 
    /// The <see cref="CalendarMonthYear" /> property's name. 
    /// </summary> 
    public const string CalendarMonthYearPropertyName = "CalendarMonthYear"; 

    private string _calendarMonthYear ; 

    /// <summary> 
    /// Sets and gets the CalendarMonthYear property. 
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary> 
    public string CalendarMonthYear 
    { 
     get 
     { 
      return _calendarMonthYear; 
     } 

     set 
     { 
      if (_calendarMonthYear == value) 
      { 
       return; 
      } 

      _calendarMonthYear = value; 
      RaisePropertyChanged(CalendarMonthYearPropertyName); 
     } 
    } 

    //button next month 
    private RelayCommand _nextMonth; 

    /// <summary> 
    /// Gets the NextMonth. 
    /// </summary> 
    public RelayCommand NextMonth 
    { 
     get 
     { 
      return _nextMonth 
       ?? (_nextMonth = new RelayCommand(
       () => 
       { 
        calendarDate = calendarDate.AddMonths(1); 
        Initialize_Calendar(calendarDate); 
       })); 
     } 
    } 

    //Button previous month 
    private RelayCommand _previousMonth; 

    /// <summary> 
    /// Gets the PreviousMonth. 
    /// </summary> 
    public RelayCommand PreviousMonth 
    { 
     get 
     { 
      return _previousMonth 
       ?? (_previousMonth = new RelayCommand(
       () => 
       { 
        calendarDate = calendarDate.AddMonths(-1); 
        Initialize_Calendar(calendarDate); 
       })); 
     } 
    } 

    /// <summary> 
    /// The <see cref="DATE" /> property's name. 
    /// </summary> 
    public const string DATEPropertyName = "DATE"; 

    private string _date; 

    /// <summary> 
    /// Sets and gets the DATE property. 
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary> 
    public string DATE 
    { 
     get 
     { 
      return _date; 
     } 

     set 
     { 
      if (_date == value) 
      { 
       return;      
      } 
      _date = value; 
      RaisePropertyChanged(DATEPropertyName); 
     } 
    } 

    public void Initialize_Calendar(DateTime date) 
    { 
     CalendarMonthYear = date.ToString("MMMM yyyy"); 
     date = new DateTime(date.Year, date.Month, 1); 
     int dayOfWeek = (int)date.DayOfWeek + 1; 
     int daysOfMonth = DateTime.DaysInMonth(date.Year, date.Month); 
     int i = 1; 
     DateCollection.Clear(); 
     for (int d = 1; d <= daysOfMonth; d++) 
     { 
      if (i >= dayOfWeek && i < (daysOfMonth + dayOfWeek)) 
      { 
       DATE = (i - dayOfWeek + 1).ToString(); 
       DateCollection.Add(DATE); 
      } 
      else 
      { 
       DATE = ""; 
       DateCollection.Add(DATE); 
       if (DATE == "") 
       { 
        daysOfMonth++; 
       } 
      } 
      i++; 
     } 
    } 

    private RelayCommand _dateClick; 

    /// <summary> 
    /// Gets the DateClick. 
    /// </summary> 
    public RelayCommand DateClick 
    { 
     get 
     { 
      return _dateClick 
       ?? (_dateClick = new RelayCommand(
       async() => 
       { 
        EventCollection.Clear(); 
        List<Event> E = await App.MobileService.GetTable<Event>().ToListAsync(); 
         foreach(Event evnt in E) 
         { 
          if (evnt.Date.Date.Equals(DateTime.Today.Date)) 
          { 
           EventCollection.Add(new Event 
            { 
             Id = evnt.Id, 
             EventName = evnt.EventName, 
             Desc = evnt.Desc, 
             Category = evnt.Category, 
             Location = evnt.Location, 
             StartingTime = evnt.StartingTime, 
             Date = evnt.Date  
            }); 
          } 

         } 
        if(EventCollection.Count == 0) 
          { 
           MessageDialog m = new MessageDialog("Empty", "No Events today!."); 
           await m.ShowAsync(); 
          } 
       })); 
     } 
    } 
} 
+0

爲什麼你需要訪問轉換器的highlightedDate對象,而不是你將不得不檢查上月變化「日期」的屬性值。您必須在月份更改時通知日期或手動設置。 –

+0

你可以幫我更多地瞭解如何通知月份更改日期或手動設置。我可以弄明白,或者我錯了。我仍然看到如何在下個月不突出顯示日期的問題 – user3411961

回答

0

我真的不明白問題的全部,但如果你需要訪問你的兩個屬性轉換器然後你需要使用多重綁定。

爲什麼在String中保存的日期爲什麼不在DateTime中轉換呢?

問候Burim Hajrizaj

相關問題