fork download
  1. Imports System.ComponentModel
  2.  
  3.  
  4. Public Class TextBox2nd
  5. Inherits System.Windows.Controls.TextBox
  6.  
  7. Shared Sub New()
  8. 'この OverrideMetadata 呼び出しは、この要素が基本クラスと異なるスタイルを提供することをシステムに通知します。
  9. 'このスタイルは themes\generic.xaml に定義されています
  10. DefaultStyleKeyProperty.OverrideMetadata(GetType(TextBox2nd), New FrameworkPropertyMetadata(GetType(TextBox)))
  11. End Sub
  12.  
  13. #Region "日付"
  14.  
  15. Public Shared ReadOnly DateValueProperty As DependencyProperty = DependencyProperty.Register( _
  16. "DateValue", _
  17. GetType(Date?), _
  18. GetType(TextBox2nd), _
  19. New PropertyMetadata(New PropertyChangedCallback(AddressOf OnDateChanged)))
  20.  
  21. Public Property DateValue() As Date?
  22. Get
  23. Return DirectCast(GetValue(DateValueProperty), Date?)
  24. End Get
  25. Set(ByVal value As Date?)
  26. SetValue(DateValueProperty, value)
  27.  
  28. End Set
  29. End Property
  30.  
  31. Private Shared Sub OnDateChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
  32. Dim textbox2 As TextBox2nd = DirectCast(d, TextBox2nd)
  33. If DirectCast(e.NewValue, Date?).HasValue Then
  34. textbox2.SetValue(TextBox2nd.TextProperty, DirectCast(e.NewValue, Date?).Value.ToString("yyyy/MM"))
  35. Else
  36. textbox2.SetValue(TextBox2nd.TextProperty, String.Empty)
  37. End If
  38. End Sub
  39.  
  40. #End Region
  41.  
  42. Private Sub TextBox2nd_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.LostFocus
  43. Dim dt As Date
  44. If Date.TryParse(Me.Text, dt) Then
  45. Me.DateValue = dt
  46. Else
  47. Me.DateValue = Nothing
  48. 'なぜかTextが空のときにこっちに来るとOnDateChangedが走らないので。
  49. Me.Text = String.Empty
  50. End If
  51. End Sub
  52. End Class
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty