1

I have three classes Assignment (abstract), CourseAssignment, and StudentAssignment. StudentAssignment and CourseAssignment both inherit from the abstract Assignment class.

I have a complex style where I have reformatted the look and feel of the WPF calendar, but I want assignments to show up in my calendar, so I have created a custom control (call it MyCalendar, which inherits from Calendar, but adds a dependency property (see code below) which allows me to bind an assignment list to the calendar.

public static DependencyProperty AssignmentsProperty =
        DependencyProperty.Register
        ("Assignments",typeof(ObservableCollection<Assignment>),typeof(Calendar));

    public ObservableCollection<Assignment> Assignments
    { 
        get {return (ObservableCollection<Assignment>)GetValue(AssignmentsProperty);} 
        set {SetValue(AssignmentsProperty, value); }
    }

Depending on which tab control I place the calendar control, the binding is going to be either to an observable collection of StudentAssignment or CourseAssignment. The calendar shouldn't really care if the assignment list is a CourseAssignment or StudentAssignment list, because all it needs is the due date and description (both properties are in the base abstract class), but herein lies my problem. I am using a IMultiValueConverter to put the assignments onto the correct day

[ValueConversion(typeof(ObservableCollection<Assignment>), typeof(ObservableCollection<Assignment>))]
public class AssignmentConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DateTime date = (DateTime)values[1];
        ObservableCollection<Assignment> assignments = new ObservableCollection<Assignment>();

        foreach (Assignment assignment in (ObservableCollection<Assignment>)values[0])
        {
            if (assignment.DueDate.Date == date)
            {
                assignments.Add(assignment);
            }
        }
        return assignments;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

What is the best way to get my binding to work? I know I want to set the binding to the observable collection, but if I do it in XAML, I get cannot convert from observablecollection(CourseAssignment) to observablecollection(Assignment) error; I need to somehow convert the collection of CourseAssignments to a collection of Assignments before I do the binding, right? What is the best way to do that?

4

0 回答 0