Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Collection dependency property "is read-only and cannot be set from markup"

I am creating a user control to display a three-month calendar. The control is based on the WPF Calendar control (WPF Toolkit 2009-06), and I want to pass several of the Calendar's properties through to corresponding properties of my user control. The user control properties are set up as Dependency Properties, and their underlying types match the types of the Calendar properties. Here is my markup:

<StackPanel>
    <toolkit:Calendar Name="MasterCalendar" 
        SelectionMode="{Binding Path=SelectionMode, Mode=OneWay}"
        SelectedDate="{Binding Path=SelectedDate, Mode=OneWayToSource}"
        SelectedDates="{Binding Path=SelectedDates, Mode=OneWayToSource}"/>
    <toolkit:Calendar Name="SlaveCalendar1" 
        DisplayDate="{Binding DisplayDate, Converter={StaticResource IncrementalMonthConverter}, ElementName=MasterCalendar, Mode=OneWay}"
        SelectionMode="{Binding Path=SelectionMode, Mode=OneWay}"
        SelectedDate="{Binding Path=SelectedDate, Mode=OneWayToSource}"
        SelectedDates="{Binding Path=SelectedDates, Mode=OneWayToSource}"/>
    <toolkit:Calendar Name="SlaveCalendar2" 
        DisplayDate="{Binding DisplayDate, Converter={StaticResource IncrementalMonthConverter}, ElementName=SlaveCalendar1, Mode=OneWay}"
        SelectionMode="{Binding Path=SelectionMode, Mode=OneWay}"
        SelectedDate="{Binding Path=SelectedDate, Mode=OneWayToSource}"
        SelectedDates="{Binding Path=SelectedDates, Mode=OneWayToSource}"/>
</StackPanel>

All of the properties bind without problem, except for the SelectedDates property. I get the following error on its binding:

'SelectedDates' property is read-only and cannot be set from markup.

I suspect that it is because the SelectedDates property is a collection, but I am not sure how to fix the problem. Can anyone enlighten me on the cause of the problem and suggest a fix? Thanks for your help.

like image 744
David Veeneman Avatar asked Jan 27 '10 14:01

David Veeneman


1 Answers

If I understand you well, you have Dependency properties in your code behind that match in name and type the properties of the Calendar Controls in your user control. You are trying to assign the SelectedDates Collection of the various Calendar Controls to the Dependency property of the same name in your code behind.

You can simply do this by a line of code:

this.SelectedDates=SlaveCalendar1.SelectedDates

In an appropriate EventHandler that fires when a selected date is added.

Even though you set the binding to OneWayToSource the SelectedDates= piece of code is an assignment. As the SelectedDates Property has no setter, it is not possible to write this piece of code.

Here you can find a link to the Calendar Control's documentation

like image 113
Dabblernl Avatar answered Oct 15 '22 11:10

Dabblernl