WPF:集合依赖属性“是只读的,不能从标记中设置”

时间:2022-09-12 08:30:19

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:

我正在创建一个用户控件来显示一个3个月的日历。该控件基于WPF日历控件(WPF Toolkit 2009-06),我想将日历的几个属性传递给用户控件的相应属性。用户控件属性被设置为依赖属性,它们的底层类型与日历属性的类型相匹配。这是我的标记:

<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属性之外,所有属性都可以毫无问题地绑定。我在它的装订上得到以下错误:

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

“SelectedDates”属性是只读的,不能从标记设置。

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.

我怀疑这是因为SelectedDates属性是一个集合,但我不确定如何修复这个问题。谁能指点我问题的原因并提出解决办法吗?谢谢你的帮助。

1 个解决方案

#1


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.

如果我很了解您,那么您的代码在名称匹配后有依赖属性,并在您的用户控件中键入日历控件的属性。您正在尝试将不同日历控件的SelectedDates集合分配给后面代码中相同名称的依赖项属性。

You can simply do this by a line of code:

你可以通过一行代码来完成:

this.SelectedDates=SlaveCalendar1.SelectedDates

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.

即使您将绑定设置为OneWayToSource, SelectedDates=一段代码也是一个赋值。由于SelectedDates属性没有setter,因此不可能编写这段代码。

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

在这里,您可以找到到Calendar控件文档的链接

#1


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.

如果我很了解您,那么您的代码在名称匹配后有依赖属性,并在您的用户控件中键入日历控件的属性。您正在尝试将不同日历控件的SelectedDates集合分配给后面代码中相同名称的依赖项属性。

You can simply do this by a line of code:

你可以通过一行代码来完成:

this.SelectedDates=SlaveCalendar1.SelectedDates

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.

即使您将绑定设置为OneWayToSource, SelectedDates=一段代码也是一个赋值。由于SelectedDates属性没有setter,因此不可能编写这段代码。

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

在这里,您可以找到到Calendar控件文档的链接