windows窗体数据绑定中的自定义逻辑 [英] Custom logic in windows forms data binding

查看:24
本文介绍了windows窗体数据绑定中的自定义逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚从使用 Knockout.js 的 Web 开发转向 windows 窗体,我正在阅读绑定,我的目标是实现类似于 Knockout 的 MVVM 架构,但在 windows 窗体中.

Just moved from web development with Knockout.js to windows forms and I'm reading up on bindings, my goal is to achieve a MVVM architecture similar to Knockout, but in windows forms.

也就是说,我在尝试在控件的绑定上应用自定义逻辑时遇到了问题,例如,我想将错误标签的 Visible 属性绑定到 ViewModel 类中名为 IsValid 的函数的结果

That said, I'm having trouble trying to apply custom logic on controls' bindings, I want for example bind the Visible property of an error label to the result of a function called IsValid in a ViewModel class

我怎样才能做到这一点?

How can I achieve this?

推荐答案

Winforms(vb.net 代码)中的下一个 DataBinding 示例有什么问题:

What wrong with the next DataBinding example in the Winforms(vb.net code):

Public Class Info
    Implements INotifyPropertyChanged

    Private _Word As string
    Public Property Word As String
        Get
            Return _Word
        End Get
        Set(value As String)
            If value.Equals(_Word) = False Then
                _Word = value
                Me.NotifyPropertyChanged("Word")
            End If
        End Set
    End Property


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

    Private Sub NotifyPropertyChanged(propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Public Sub New(newWord As String)
        Me.Word = newWord
    End Sub

    Public Function IsValid() As Boolean
        Return (String.IsNullOrEmpty(Me.Word) = False)
    End Function
End Class

和表格代码

Public Class frmInfo
    Private _info As Info

    Public Sub New(inInfo As String)
        InitializeComponent()
        _info= New Info(inInfo)
    End Sub

    Private Sub frmInfo_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.txtID.DataBindings.Add("Text", _info, "Word") 'Add DataBinding for Word property
        'Binding label Visible property to result of the IsValid function
        Dim bind As New Binding("Visible", _info, "Word")
        AddHandler bind.Format, Sub(obj As Object, args As ConvertEventArgs) args.Value = _raha.IsValid()
        Me.lblEven.DataBindings.Add(bind)
    End Sub

End Class

这篇关于windows窗体数据绑定中的自定义逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆