GlassBeaver
GlassBeaver

Reputation: 332

WPF C# How to bind and set a List in custom model class in WPF with x:array

I'm having a problem with my first forays into WPF territory, trying to populate a list in a custom model class from XAML.

The exception I'm getting is:

Additional information: 'Set property 'Project.Model.GanttModel.Tasks' threw an exception.' Line number '19' and line position '27'.

This is the Model:GanttModel... line.

My question is how to populate the elements of the "GanttModelTest" model's "TasksTest" list?

My model class:

    public class GanttModel : DependencyObject {
    public static readonly DependencyProperty TasksProperty = DependencyProperty.Register("Tasks", typeof(ObservableCollection<Task>), typeof(GanttModel));
    public static readonly DependencyProperty ChartDateRangeProperty = DependencyProperty.Register("ChartDateRange", typeof(DateTimeRange), typeof(GanttModel));

    public DateTimeRange ChartDateRange {
        get {
            return GetValue(ChartDateRangeProperty) as DateTimeRange;
        }

        set {
            SetValue(ChartDateRangeProperty, value);
        }
    }

    public ObservableCollection<Task> Tasks {
        get {
            return GetValue(TasksProperty) as ObservableCollection<Task>;
        }

        set {
            SetValue(TasksProperty, value);
        }
    }

    public GanttModel() {
        this.Tasks = new ObservableCollection<Task>();
    }
}

The relevant parts of my XAML:

    <UserControl.Resources>
    <x:Array x:Key="TasksTest" Type="Model:Task">
        <Model:Task Name="Task 1" Start="07-20-2013" Finish="08-01-2013" />
        <Model:Task Name="Task 2" Start="07-19-2013" Finish="07-31-2013" />
        <Model:Task Name="Task 3" Start="07-18-2013" Finish="07-30-2013" />
        <Model:Task Name="Task 4" Start="07-17-2013" Finish="07-29-2013" />
        <Model:Task Name="Task 5" Start="07-16-2013" Finish="07-28-2013" />
    </x:Array>

    <Model:GanttModel x:Key="GanttModelTest"
                      ChartDateRange="07-01-2013,08-01-2013"
                      Tasks="{StaticResource TasksTest}" />

    etc...etc...etc...

    <ListBox x:Name="GanttTasks"
             ItemsSource="{StaticResource GanttModelTest}"
             ItemTemplate="{StaticResource TaskTemplate}"/>

    </UserControl>

Upvotes: 3

Views: 404

Answers (1)

nemesv
nemesv

Reputation: 139778

Your TasksProperty type is ObservableCollection<Task> but you try to assign an Task[] to it. Because there is no implicit convention between the two type that is why the Set throws the exception.

You have two options:

Change your TasksProperty to use Task[] instead of ObservableCollection<Task>.

Or build an ObservableCollection<Task> in XAML, but because you cannot use XAML2009 features in WPF so cannot directly create an ObservableCollection in WPF XAML. See also the documenation: Notes on XAML Usage.

What you can do is to derive from ObservableCollection

public class TaskCollection : ObservableCollection<Task> { }

And now you can use this non generic version in XAML:

<Model:TaskCollection x:Key="TasksTest">
        <Model:Task Name="Task 1" Start="07-20-2013" Finish="08-01-2013" />
        <Model:Task Name="Task 2" Start="07-19-2013" Finish="07-31-2013" />
        <Model:Task Name="Task 3" Start="07-18-2013" Finish="07-30-2013" />
        <Model:Task Name="Task 4" Start="07-17-2013" Finish="07-29-2013" />
        <Model:Task Name="Task 5" Start="07-16-2013" Finish="07-28-2013" />
</Model:TaskCollection>

Upvotes: 2

Related Questions