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)

データグリッドでは値は変更されません。逆も同じ問題です。startTime値をwpfコントロールに取得しません。

アプリケーションがコンパイルされてエラーなしで実行されても、バインディングが機能していないようです。

誰かが私を助けることができますか?

敬具

4

0 に答える 0