我有一个只读的依赖属性名为" Periods"。我已使用ContentPropertyAttribute将其设置为content属性。但是,由于Periods是集合类型依赖项属性,因此必须在构造函数中初始化它以确保不形成单例。我试过这个,但是在运行时,尽管在XAML中设置,但属性似乎没有值。我推断我必须在XAML设置之后初始化属性,然后重写它。我在我的假设中是对的,如果是这样,我可以在XAML文件中设置之前初始化该属性。
编辑1: 当初始化值不为空或在控件加载后手动设置值时,控件按预期工作,这使我认为我的假设是正确的。任何帮助将不胜感激。
以下是我的代码的相关部分:
<ContentProperty("Periods")>
Public Class Day
Inherits System.Windows.Controls.Control
#Region "Constructers"
Shared Sub New()
'This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
'This style is defined in themes\generic.xaml
DefaultStyleKeyProperty.OverrideMetadata(GetType(Day), New FrameworkPropertyMetadata(GetType(Day)))
End Sub
Public Sub New()
MyBase.New()
Me.SetValue(PeriodsPropertyKey, New ObservableCollection(Of Period))
End Sub
Protected Overrides Sub OnInitialized(e As EventArgs)
MyBase.OnInitialized(e)
End Sub
#End Region
#Region "Properties"
Public ReadOnly Property Periods() As ObservableCollection(Of Period)
Get
Return Me.GetValue(PeriodsProperty)
End Get
End Property
Private Shared ReadOnly PeriodsPropertyKey As DependencyPropertyKey = DependencyProperty.RegisterReadOnly("Periods", GetType(ObservableCollection(Of Period)), GetType(Day), New FrameworkPropertyMetadata(Nothing))
Public Shared ReadOnly PeriodsProperty As DependencyProperty = PeriodsPropertyKey.DependencyProperty
#End Region
这是XAML:
<local:Timetable Margin="50,25,21,68" UseLayoutRounding="True" PixelToMinuteRatio="2" StartTime="9:00"
x:Name="Timetable1">
<local:Day DayName="Sunday">
<local:Period Background="#72000000" VerticalAlignment="Top" Day="1" StartTime="9:00"
EndTime="10:20" Margin="0,0,1,0" TimetableStartTime="{Binding ElementName=Timetable1, Path=StartTime}"
TimetableEndTime="{Binding ElementName=Timetable1, Path=EndTime}" />
</local:Day>
<local:Day DayName="Monday">
<local:Period Background="#72000000" VerticalAlignment="Top" Day="1" StartTime="9:00"
EndTime="10:20" Margin="0,0,1,0" TimetableStartTime="{Binding ElementName=Timetable1, Path=StartTime}"
TimetableEndTime="{Binding ElementName=Timetable1, Path=EndTime}" />
<local:Period Background="#72000000" VerticalAlignment="Top" Day="1" StartTime="10:20"
EndTime="11:00" Margin="0,0,1,0" TimetableStartTime="{Binding ElementName=Timetable1, Path=StartTime}"
TimetableEndTime="{Binding ElementName=Timetable1, Path=EndTime}" />
</local:Day>
<local:Day DayName="Tuesday">
<local:Period Background="#72000000" VerticalAlignment="Top" Day="1" StartTime="9:00"
EndTime="10:20" Margin="0,0,1,0" TimetableStartTime="{Binding ElementName=Timetable1, Path=StartTime}"
TimetableEndTime="{Binding ElementName=Timetable1, Path=EndTime}" />
<local:Period Background="#72000000" VerticalAlignment="Top" Day="1" StartTime="10:20"
EndTime="11:00" Margin="0,0,1,0" TimetableStartTime="{Binding ElementName=Timetable1, Path=StartTime}"
TimetableEndTime="{Binding ElementName=Timetable1, Path=EndTime}" />
<local:Period Background="#72000000" VerticalAlignment="Top" Day="1" StartTime="11:15"
EndTime="11:55" Margin="0,0,1,0" TimetableStartTime="{Binding ElementName=Timetable1, Path=StartTime}"
TimetableEndTime="{Binding ElementName=Timetable1, Path=EndTime}" />
</local:Day>
</local:Timetable>