Initialize dependency property before it is set through XAML

1.1k views Asked by At

I have a read only dependency property named "Periods". I have set this is as the content property using the ContentPropertyAttribute. However, as Periods is a collection type dependency property it must be initialized in the constructor to ensure that a singleton is not formed. I tried this but the property does not seem to have a value when run despite being set in XAML. I reasoned that I must be initializing the property after it has already been set by the XAML and so overwriting it. Am I right in my assumption and if so where can I initialize the property before it has been set in the XAML file.

Edit 1: When the initialization value is not empty or a value is set manually after the control is loaded the control works as expected which makes me think that my assumption is correct. Any help would be much appreciated.

Here is the relevant bits of my code:

<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

Here is the 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>
0

There are 0 answers