Silverlight ValidationSummary 省略 UserControl 绑定的属性的 MessageHeader

发布于 2024-12-13 12:09:33 字数 3772 浏览 1 评论 0原文

我使用 DependencyProperty 定义了自定义 UserControl。我的页面包含 UserControl、文本框和 ValidationSummary。该页面使用 XAML 实例化 Textbox 和 UserControl,将它们的 DependencyProperties 绑定到单个 ViewModel 的 Properties。 ValidationSummary 按“请求描述”文本框的预期工作:

  1. MessageHeader(粗体)显示来自绑定属性的 DisplayAttribute 的 Name 属性的文本。
  2. 消息(非粗体)显示绑定属性引发的异常文本。

    <显示(名称:="请求描述")> _
    公共属性 RequestDescription() 作为字符串
      得到
          ……
      结束获取
      设置(ByVal 值作为字符串)
          ……
          如果 value.ToLower.Contains("zombie") 那么
              抛出新异常(“我们不是说 zed 这个词。”)
          结束如果
          ……
      结束组
    结束财产
    

但是,“舰队单元”用户控件的 ValidationSummary 缺少 MessageHeader。

val sum

当我检查 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:

  1. MessageHeader (bolded) shows text from bound property’s DisplayAttribute’s Name property.
  2. 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.

val sum

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文