User logic in windows forms data binding

I just switched from web development using Knockout.js to Windows forms, and I read about bindings, my goal is to create an MVVM architecture similar to Knockout but in Windows forms.

However, I am having problems trying to apply custom logic to control bindings, for example, I want to bind the Visible property of the error label to the result of the IsValid function in the ViewModel class

How can i achieve this?

0
winforms mvvm
source share
1 answer

What happened to the following DataBinding example in 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 

And the form code

 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 
+1
source share

All Articles