Silverlight ValidationSummary 省略 UserControl 绑定的属性的 MessageHeader
我使用 DependencyProperty 定义了自定义 UserControl。我的页面包含 UserControl、文本框和 ValidationSummary。该页面使用 XAML 实例化 Textbox 和 UserControl,将它们的 DependencyProperties 绑定到单个 ViewModel 的 Properties。 ValidationSummary 按“请求描述”文本框的预期工作:
- MessageHeader(粗体)显示来自绑定属性的 DisplayAttribute 的 Name 属性的文本。
消息(非粗体)显示绑定属性引发的异常文本。
<显示(名称:="请求描述")> _ 公共属性 RequestDescription() 作为字符串 得到 …… 结束获取 设置(ByVal 值作为字符串) …… 如果 value.ToLower.Contains("zombie") 那么 抛出新异常(“我们不是说 zed 这个词。”) 结束如果 …… 结束组 结束财产
但是,“舰队单元”用户控件的 ValidationSummary 缺少 MessageHeader。
当我检查 ValidationSummary 的错误属性时,我发现 UserControl 的错误有:
- MessageHeader=Nothing
- Sources.Count =1
- Sources(0).Control=MyUserControl
- Sources(0).PropertyName=Nothing
相反,文本框的错误具有与 MessageHeader 和 Sources(0).PropertyName 匹配的DisplayAttribute 的 Name 属性。
我已将带有 Name 的 DisplayAttribute 放置在 UserControl 绑定到的 ViewModel 属性上。我尝试将 DisplaceAttributes 放置在 UserControl 的 DependencyProperty 中以及 DependencyProperty 注册中指定的属性中。这些都不会流向 ValidationSummary 的 ValidationSummaryItemSource.PropertyName。
任何人都可以提供有关使 ValidationSummary 显示由 UserControl 绑定的属性的 MessageHeader 的提示吗?
更多信息(美国东部时间 2011 年 11 月 2 日下午 5:15):
单击 ValidationSummary 中列出的任一错误都会导致相关控件获得焦点。单击文本框时,消息将显示在文本框的右侧以及 ValidationSummary 中。 (未显示。)单击 UserControl 时,消息仅出现在 ValidationSummary 中。
更多信息(美国东部时间 2011 年 11 月 9 日上午 10:14):
kmacmahon:希望这是您所需要的。感谢您的帮助!
Function ValidateEntry(ByVal fieldname As String, ByRef value As Object) _
As Object
Dim ctx As New ValidationContext(Me, Nothing, Nothing)
ctx.MemberName = fieldname
Validator.ValidateProperty(value, ctx)
Return value
End Function
。
<Display(Name:="Fleet Unit ID")> _
<Required(ErrorMessage:="Fleet Unit is required, but not supplied.")> _
Public Property FleetUnitID As Integer?
Get
Return _Incident.EFleetUnitID
End Get
Set(ByVal value As Integer?)
Const propname As String = "FleetUnitID"
If Not Equals(_Incident.EFleetUnitID, value) Then
If value Is Nothing Then
_Incident.EFleetUnitID = Nothing
Else
_Incident.EFleetUnitID = ValidateEntry(propname, value)
End If
NotifyChangeUpdate(propname)
ElseIf value Is Nothing AndAlso FleetUnitIDRequired Then
ValidateEntry(propname, value)
End If
End Set
End Property
。
<Display(Name:="Request Description")> _
<Required(ErrorMessage:="Please describe your request.")> _
<StringLength(8000)> _
Public Property RequestDescription() As String
Get
Return Incident.RequestDescription
End Get
Set(ByVal value As String)
Const propname As String = "RequestDescription"
If Not Equals(Incident.RequestDescription, value) Then
Incident.RequestDescription = ValidateEntry(propname, value)
NotifyChangeUpdate(propname)
End If
If RequestSpecificReference IsNot Nothing _
AndAlso RequestSpecificReference.Template > "" Then
If value = RequestSpecificReference.Template Then
Throw New Exception("Please provide information" _
& " requested in description field.")
End If
End If
If value.ToLower.Contains("zombie") Then
Throw New Exception("We're not saying the zed word.")
End If
NotifyPropertyChanged("SubmitButtonVisibility")
End Set
End Property
I’ve defined a custom UserControl with DependencyProperty. My page contains the UserControl, a Textbox, and a ValidationSummary. Using XAML, the page instantiates the Textbox and UserControl, binding their DependencyProperties to Properties of a single ViewModel.
The ValidationSummary works as expected for the "Request Description" Textbox:
- MessageHeader (bolded) shows text from bound property’s DisplayAttribute’s Name property.
Message (non-bolded) shows text of exception thrown by bound property.
<Display(Name:="Request Description")> _ Public Property RequestDescription() As String Get … End Get Set(ByVal value As String) … If value.ToLower.Contains("zombie") Then Throw New Exception("We're not saying the zed word.") End If … End Set End Property
However, the ValidationSummary for the "Fleet Unit" UserControl lacks a MessageHeader.
When I examine the ValidationSummary’s errors property, I find that the error for the UserControl has:
- MessageHeader=Nothing
- Sources.Count=1
- Sources(0).Control=MyUserControl
- Sources(0).PropertyName=Nothing
In contrast, the error for the Textbox has both MessageHeader and Sources(0).PropertyName matching the DisplayAttribute’s Name property.
I’ve placed a DisplayAttribute with Name on the ViewModel property to which the UserControl is bound. I’ve tried placing DisplaceAttributes in the UserControl’s DependencyProperty as well as the Property named in the DependencyProperty’s registration. None of these flow through to the ValidationSummary’s ValidationSummaryItemSource.PropertyName.
Can anyone offer a tip on making ValidationSummary show a MessageHeader for a property bound by a UserControl?
More info (11/2/2011 5:15 PM ET):
Clicking on either of the errors listed in ValidationSummary causes the related control to be focused. When the Textbox is clicked, the Message is displayed to the right of the Textbox as well as in the ValidationSummary. (Not shown.) When the UserControl is clicked the Message only appears in the ValidationSummary.
More info (11/9/2011 10:14 AM ET):
kmacmahon: Hopefully this what you needed. Thanks for your help!
Function ValidateEntry(ByVal fieldname As String, ByRef value As Object) _
As Object
Dim ctx As New ValidationContext(Me, Nothing, Nothing)
ctx.MemberName = fieldname
Validator.ValidateProperty(value, ctx)
Return value
End Function
.
<Display(Name:="Fleet Unit ID")> _
<Required(ErrorMessage:="Fleet Unit is required, but not supplied.")> _
Public Property FleetUnitID As Integer?
Get
Return _Incident.EFleetUnitID
End Get
Set(ByVal value As Integer?)
Const propname As String = "FleetUnitID"
If Not Equals(_Incident.EFleetUnitID, value) Then
If value Is Nothing Then
_Incident.EFleetUnitID = Nothing
Else
_Incident.EFleetUnitID = ValidateEntry(propname, value)
End If
NotifyChangeUpdate(propname)
ElseIf value Is Nothing AndAlso FleetUnitIDRequired Then
ValidateEntry(propname, value)
End If
End Set
End Property
.
<Display(Name:="Request Description")> _
<Required(ErrorMessage:="Please describe your request.")> _
<StringLength(8000)> _
Public Property RequestDescription() As String
Get
Return Incident.RequestDescription
End Get
Set(ByVal value As String)
Const propname As String = "RequestDescription"
If Not Equals(Incident.RequestDescription, value) Then
Incident.RequestDescription = ValidateEntry(propname, value)
NotifyChangeUpdate(propname)
End If
If RequestSpecificReference IsNot Nothing _
AndAlso RequestSpecificReference.Template > "" Then
If value = RequestSpecificReference.Template Then
Throw New Exception("Please provide information" _
& " requested in description field.")
End If
End If
If value.ToLower.Contains("zombie") Then
Throw New Exception("We're not saying the zed word.")
End If
NotifyPropertyChanged("SubmitButtonVisibility")
End Set
End Property
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论