如何在类中访问视图模型中声明的属性
本文关键字:模型 声明 属性 视图 访问 | 更新日期: 2023-09-27 18:08:46
我创建了一个日历,我的代码在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();
}
}));
}
}
}
我真的不明白整个问题,但如果你需要在你的转换器上访问两个属性,那么你需要使用多绑定。
为什么日期保存在字符串为什么不转换为DateTime?
敬祝Burim Hajrizaj