简体   繁体   English

如何访问在类的viewmodel中声明的属性?

[英]How to access a property declared in the viewmodel in a class?

What I did I created a calendar and my codes are in the view model and to be able to highlight the dates I created a class inherited the IValueConverter . 我创建了日历,代码在view model并且能够突出显示创建自IValueConverter的类的日期。 Now the error is that when i change the month in my emulator and say the current date it the 20th, even though it's a different month the date is highlighted. 现在的错误是,当我在模拟器中更改月份并说当前日期为20号时,即使它是不同的月份,日期也会被突出显示。 When I change the month there is a property that gets updated in my view model . 当我更改月份时, view model中会更新一个property So how can I get access to this property in my converter class . 因此,如何在我的converter class访问此property

Here are my codes: 这是我的代码:

Complete converter class: 完整的转换器类:

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();
    }
}

Complete view model: 完整的视图模型:

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();
                            }
                }));
        }
    }
}

I really don't understand the whole problem but if you need to access two Properties on you Converter then you need to use multi-binding. 我真的不了解整个问题,但是如果您需要访问Converter上的两个属性,则需要使用多重绑定。

Why is the date saved in String why not convert it in DateTime? 为什么将日期保存在String中,为什么不在DateTime中将其转换呢?

Best regards Burim Hajrizaj 最好的问候Burim Hajrizaj

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM