1

我有一个名为“Periods”的只读依赖属性。我已使用 ContentPropertyAttribute 将其设置为内容属性。但是,由于 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>
4

0 回答 0