2

我很难绑定 wpf 自定义控件的属性。这是我的 xaml 自定义数据网格单元格的摘录,其数据上下文是列表(日期):

<DataGridTemplateColumn Header="Start" MinWidth="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <timePicker:CustomTimePicker selectedTime="{Binding Path=startDate}" MinWidth="100" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>

如您所见,我尝试将我的 List(Of Date) 的 startDate 属性绑定到我的自定义 wpf 控件 TimePicker 中的 selectedTime 依赖项属性。这是具有 selectedDate 依赖属性的 customtimepicker 类的定义:

Public Class CustomTimePicker

Private _hours As String
Private _minutes As String


Private _hoursChoices As List(Of String)
Private _minutesChoices As List(Of String)

Public Shared selectedTimeProperty As DependencyProperty = DependencyProperty.Register("selectedTime", GetType(Date), GetType(CustomTimePicker), New FrameworkPropertyMetadata(DateTime.MinValue, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, New PropertyChangedCallback(AddressOf CustomTimePicker.OnSelectedTimeChanged)))

Public Property selectedTime() As Date
    Get
        Return DirectCast(GetValue(selectedTimeProperty), Date)
    End Get
    Set(value As Date)
        SetValue(selectedTimeProperty, value)
    End Set
End Property

Public Property hours As String
    Get
        Return _hours
    End Get
    Set(value As String)
        _hours = value
    End Set
End Property

Public Property minutes As String
    Get
        Return _minutes
    End Get
    Set(value As String)
        _minutes = value
    End Set
End Property

Public Property hoursChoices As List(Of String)
    Get
        Return _hoursChoices
    End Get
    Set(value As List(Of String))
        _hoursChoices = value
    End Set
End Property

Public Property minutesChoices As List(Of String)
    Get
        Return _minutesChoices
    End Get
    Set(value As List(Of String))
        _minutesChoices = value
    End Set
End Property

Protected Shared Sub OnSelectedTimeChanged(ByVal obj As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
    Console.WriteLine("@@@@@@@")
    Console.WriteLine(CType(obj, CustomTimePicker).selectedTime.ToString)
End Sub

Protected Shared Sub OnCoerce()

End Sub

Protected Sub OnSelectedHourChanged(obj As Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles hourComboBox.SelectionChanged
    Dim minute As Integer = 0

    Dim result As Date = New Date(2000, 1, 1, hourComboBox.SelectedIndex, selectedTime.Minute, 0)
    selectedTime = result

    Console.WriteLine("---")
    Console.WriteLine(result.ToString)
    Console.WriteLine(selectedTime.ToString)

End Sub

Protected Sub OnSelectedMinuteChanged(obj As Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles minutesComboBox.SelectionChanged
    Dim minute As Integer = 0
    Console.WriteLine("bbbbbb")
    Console.WriteLine(selectedTime.ToString)
    Select Case minutesComboBox.SelectedIndex
        Case 0
            minute = 0
        Case 1
            minute = 15
        Case 2
            minute = 30
        Case 3
            minute = 45
    End Select

    Dim result As Date = New Date(2000, 1, 1, selectedTime.Hour, minute, 0)
    selectedTime = result
End Sub


Protected Overrides Sub OnInitialized(e As System.EventArgs)
    MyBase.OnInitialized(e)
    _hoursChoices = New List(Of String)
    _minutesChoices = New List(Of String)

    Console.WriteLine("aaaaaaa")
    Console.WriteLine(selectedTime.ToString)

    For i As Integer = 0 To 23
        _hoursChoices.Add(i.ToString)
    Next
    _minutesChoices.Add("00")
    _minutesChoices.Add("15")
    _minutesChoices.Add("30")
    _minutesChoices.Add("45")

    hourComboBox.ItemsSource = hoursChoices
    hourComboBox.DisplayMemberPath = hours
    hourComboBox.SelectedValuePath = hours
    hourComboBox.SelectedValue = hours

    minutesComboBox.ItemsSource = minutesChoices
    minutesComboBox.DisplayMemberPath = minutes
    minutesComboBox.SelectedValuePath = minutes
    minutesComboBox.SelectedValue = minutes



    Select Case selectedTime.Minute
        Case 0 To 14
            minutesComboBox.SelectedIndex = 0
        Case 15 To 29
            minutesComboBox.SelectedIndex = 1
        Case 30 To 44
            minutesComboBox.SelectedIndex = 2
        Case 45 To 59
            minutesComboBox.SelectedIndex = 3
    End Select

    hourComboBox.SelectedIndex = selectedTime.Hour

End Sub

结束类

如果我更改 selectedTime 属性,例如

selectedTime = New Date(2000,1,1,23,59,0)

数据网格中的值不会改变。反过来也是同样的问题。我没有得到 wpf 控件的 startTime 值。

即使应用程序编译并运行无错误,绑定似乎也确实不起作用。

有人可以帮助我吗?

亲切的问候

4

0 回答 0