【问题标题】:How to get IDataErrorInfo to propagate error messages from class to the UI?如何让 IDataErrorInfo 将错误消息从类传播到 UI?
【发布时间】:2014-09-22 11:18:38
【问题描述】:

我正在使用 VB.NET 2012 项目类型是(Windows 窗体应用程序)而不是(WPF 应用程序)

From 的 load 事件在 TextBox 和 Class1 中的“Age”属性之间创建一个数据绑定

Public Class Form1
Private _Class1 As New Class1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.TextBox1.DataBindings.Add("Text", _Class1, "Age", False, DataSourceUpdateMode.OnPropertyChanged)
    End Sub
End Class

随着 TextBox1 的 Text 属性发生变化,下面 Class1 中的 'Age' 属性也会发生变化,如果有其他东西改变了 Age 属性,例如SetVal Class1 底部的过程,更改会传播回 UI

我遇到的问题是让IDataErrorInfo 以类似的方式工作,错误可以传播回 UI,最好像 ErrorProvider 一样TextBox1 右边有个小红点?找不到明确的 VB 示例。有人可以帮忙吗?

Imports System.ComponentModel

Public Class Class1
    Implements INotifyPropertyChanged, IDataErrorInfo

    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Private m_age As Integer
    Public Property Age() As Integer
        Get
            Return m_age
        End Get
        Set(ByVal value As Integer)
            m_age = value
            OnPropertyChanged("Age")
        End Set
    End Property

    Public ReadOnly Property [Error]() As String Implements IDataErrorInfo.Error
        Get
            Return ""
        End Get
    End Property
    Default Public ReadOnly Property Item(ByVal name As String) As String Implements IDataErrorInfo.Item
        Get
            Dim result As String = Nothing
            If name = "Age" Then
                If Me.m_age < 0 OrElse Me.m_age > 150 Then
                    result = "Age must not be less than 0 or greater than 150."
                    ' NOTE: Flow arrives here but nothing happens, nothing is sent back to the form, 
                    ' I want this to trigger the displaying of an ErrorProvider, next to TextBox1 in the UI
                    Debug.WriteLine(m_age.ToString)
                End If
            End If
            Return result
        End Get
    End Property


    Private Sub OnPropertyChanged(Name As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Name))
    End Sub

    Public Sub SetVal()
        Age = 0  ' NOTE: this works, the value is propagated back to the form's TextBox1
    End Sub

End Class

【问题讨论】:

    标签: vb.net data-binding inotifypropertychanged idataerrorinfo


    【解决方案1】:

    一位同事与我分享了这个。我所缺少的只是一个错误提供程序,它的数据源设置为 _Class1。 . .现在错误条件会动态传播回 Text 控件,无需任何额外代码

    Public Class Form1
    
    Private _Class1 As New Class1
    Private errorProv As New ErrorProvider ' ** Here
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Me.TextBox1.DataBindings.Add("Text", _Class1, "Age", False,     DataSourceUpdateMode.OnPropertyChanged)
            errorProv.ContainerControl = Me ' ** Here
            errorProv.DataSource = _Class1  ' ** and Here
        End Sub
    End Class
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-13
      • 2012-08-28
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      相关资源
      最近更新 更多