【发布时间】:2014-04-27 03:28:51
【问题描述】:
我一直在研究这个问题一段时间,我想在我的 DatePicker 中添加一个验证规则,以确保 SelectedDate 介于两个日期(上限和下限)之间。 我还希望这两个限制是绑定到其他元素的数据,例如我与员工一起工作的示例在他们加入公司之前不能有出生日期。 这是我的课程: 限制日期:
Public Class LimitDates
Inherits DependencyObject
Public Shared ReadOnly UpperLimitDateProperty As DependencyProperty
Public Shared ReadOnly LowerLimitDateProperty As DependencyProperty
Shared Sub New()
Dim metadata As New FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.None)
UpperLimitDateProperty = DependencyProperty.Register("UpperLimitDate", GetType(Date), GetType(LimitDates))
LowerLimitDateProperty = DependencyProperty.Register("LowerLimitDate", GetType(Date), GetType(LimitDates))
End Sub
Public Property UpperLimitDate As Nullable(Of DateTime)
Get
Return CType(GetValue(UpperLimitDateProperty), Nullable(Of DateTime))
End Get
Set(value As Nullable(Of DateTime))
SetValue(UpperLimitDateProperty, value)
End Set
End Property
Public Property LowerLimitDate As Nullable(Of DateTime)
Get
Return CType(GetValue(LowerLimitDateProperty), Nullable(Of DateTime))
End Get
Set(value As Nullable(Of DateTime))
SetValue(LowerLimitDateProperty, value)
End Set
End Property
End Class
日期验证规则:
Public Class DateValidationRule
Inherits ValidationRule
Public Property FutureDateAllowed As Boolean = True
Public Property LimitDates As LimitDates
Public Overloads Overrides Function Validate(value As Object, cultureInfo As CultureInfo) As Windows.Controls.ValidationResult
Try
Dim d As Date = CDate(value)
If Not FutureDateAllowed And d > Now Then
Return New System.Windows.Controls.ValidationResult(False, "Future dates not allowed")
End If
If Not LimitDates Is Nothing Then
If Not LimitDates.LowerLimitDate Is Nothing Then
If d < LimitDates.LowerLimitDate Then
Return New System.Windows.Controls.ValidationResult(False, "Date must less then " & LimitDates.LowerLimitDate)
End If
End If
If Not LimitDates.UpperLimitDate Is Nothing Then
If d > LimitDates.UpperLimitDate Then
Return New System.Windows.Controls.ValidationResult(False, "Date must not be behond " & LimitDates.UpperLimitDate)
End If
End If
End If
Catch ex As Exception
Return New System.Windows.Controls.ValidationResult(False, "Not in correct format, please input a correct date. Eg. 23-04-2012")
End Try
' If hasn't returned an error already, must be okay
Return New System.Windows.Controls.ValidationResult(True, Nothing)
End Function
End Class
EmployeeView 的 XAML 标记:
<Label Style="{StaticResource EditViewLabel}" Grid.Row="0">Birth Date</Label>
<DatePicker Grid.Row="0" Grid.Column="1" x:Name="dtPkBirthDate" DisplayDateEnd="{Binding ElementName=dtPkStartDate, Path=SelectedDate}">
<DatePicker.SelectedDate>
<Binding Path="Employee.DateOfBirth">
<Binding.ValidationRules>
<local:DateValidationRule FutureDateAllowed="True">
<local:DateValidationRule.LimitDates>
<local:LimitDates UpperLimitDate="4/5/2010" />
</local:DateValidationRule.LimitDates>
</local:DateValidationRule>
</Binding.ValidationRules>
</Binding>
</DatePicker.SelectedDate>
</DatePicker>
在我看来一切都很好,如果日期是硬编码的,它确实可以工作,但是当我尝试绑定到另一个控件或 DataContext 的属性时,永远不会设置限制日期值,只有默认值12:00 AM 出现在调试器中。
另外,我如何设置和检查日期是否为空(VB.net 不是 C#,我已经习惯了)?
谢谢。 卢克
编辑:
好的,所以我想我可能已经弄清楚了为什么,在阅读了 Binding ElementName. Does it use Visual Tree or Logical Tree 和 http://social.msdn.microsoft.com/Forums/vstudio/en-US/e359b99f-e864-4e9e-b81e-2692f240598f/binding-to-object-in-template-in-another-visual-tree?forum=wpf 之后,我认为 DateValidationRule 上的绑定可能找不到 ElementName,因为这两个元素都是兄弟姐妹,所以在不同的视觉树..?
我会继续研究
【问题讨论】:
-
用户在加入公司之前不能过生日听起来很有趣:)
-
出生年月日,未出生不能加入公司:P
-
不要发布你的整个代码。只需发布相关代码。检查this post。每次都不安全,读者也很难阅读。
-
我想我找到了一个答案“ValidationRule 不是依赖对象(也不是在元素树中),所以你不能在它上面设置依赖属性的绑定(如果你可以)。” [链接] (social.msdn.microsoft.com/Forums/vstudio/en-US/…) 但让我们看看是否有其他方法可以做到这一点
标签: wpf vb.net xaml binding datepicker