【发布时间】:2015-08-20 05:43:08
【问题描述】:
我做了什么我创建了一个日历,我的代码在view model 中,并且能够突出显示我创建一个继承IValueConverter 的类的日期。现在的错误是,当我在模拟器中更改月份并说当前日期为 20 日时,即使它是不同的月份,日期也会突出显示。当我更改月份时,property 会在我的view model 中更新。那么如何在我的converter class 中访问这个property。
这是我的代码:
完整的转换器类:
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();
}
}));
}
}
}
【问题讨论】:
-
为什么您需要访问 Converter 中的 highlightDate 对象,而不是您必须检查 Month 更改的“Date”属性值。您必须通知日期更改或手动设置。
-
你能帮我更多关于如何通知日期更改或手动设置。我可以弄清楚,或者我弄错了。我仍然看到如何不突出显示下个月的日期的问题
标签: c# windows-phone-8.1 mvvm-light ivalueconverter