10

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.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
David Veeneman
  • 18,912
  • 32
  • 122
  • 187

1 Answers1

2

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

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
Dabblernl
  • 15,831
  • 18
  • 96
  • 148
  • Here is where I am puzzled: If I am passing the value from, say, SlaveCalendar1 to my user control property, I am only reading SlaveCalendar1.SelectedDates; I am writing to MyUserControl.SelectedDates, which I have defined with a setter. In other words, MyUserControl.SelectedDates isn't read-only, and that's what I am writing to. Why doesn't that work? – David Veeneman Jan 27 '10 at 18:41
  • How are you passing the SlaveCalendar1.SelectedDates to this.SelectedDates? I understand you are doing it through setting the Mode of the Binding to OneWayToSource. It seems that this line of code is still interpreted as an assignment though,for which you need a setter. There are plenty workarounds! – Dabblernl Jan 27 '10 at 19:03
  • 2
    So, if I understand correctly, I can't bind a read-only property, even if the binding is OneWayToSource; that is, a binding that only reads from the read-only property? Thanks – David Veeneman Jan 27 '10 at 20:30