0

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.

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?

Eduardo Wada
  • 2,606
  • 19
  • 31
  • 1
    winforms doesn't support MVVM (nor anything useful). You're looking for WPF. – Federico Berasategui Oct 01 '14 at 04:31
  • I already have legacy code so I'm not moving unless there's a cheap way of converting, however, just a decent way of doing a custom binding would be a huge step forward – Eduardo Wada Oct 01 '14 at 10:47
  • 1
    Alright, post your answer explaining what's missing in windows forms and why the missing features are impossible to achieve and I'll accept it – Eduardo Wada Oct 01 '14 at 12:21

1 Answers1

1

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

And code of the form

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
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • sure... you can also conveniently ignore the parts of MVVM which are not achievable in winforms, such as Data Templating. Sidestepping that doesn't make winforms less useless though. – Federico Berasategui Oct 03 '14 at 06:14
  • One problem with this code is that it's actually binding to the property "Word", if I had another property that could also affect the result of the "IsValid" function that one wouldn't be bound. – Eduardo Wada Oct 07 '14 at 11:07
  • One interesting approach I found for this matter though, was to turn "IsValid" into a readonly property, bind to it, and raise Me.NotifyPropertyChanged("IsValid") from all other setters – Eduardo Wada Oct 07 '14 at 11:09
  • Right, or you can raise `Me.NotifyPropertyChanged("IsValid")` inside of `IsValid` property, but set value for this in every Setter of your properties included in the "IsValid" group: `Me.IsValid = Me.IsValuesValid()` – Fabio Oct 07 '14 at 13:07
  • @Fabio I didn't see your comment before, for some reason... LOL... you gotta be kidding... I'm referring to things like [this](http://stackoverflow.com/questions/15532639/complex-ui-inside-listboxitem), which are clearly not supported and require tons of horrible hacks in archaic useless deprecated platforms like winforms... – Federico Berasategui Oct 09 '14 at 00:59
  • @HighCore actually, I already have that functionality in a single line of code: somePanel.Repeat((ObservableCollection)collection); "Repeat" Is an extension method I made that repeats some usercontrol for each list in the collection, and binds it to the corresponding viewmodel, it doesn't contain a horrible hacks, I only wrote this method once, and it's a single line I have to add to the constructor – Eduardo Wada Oct 13 '14 at 11:35
  • @EduardoWada Nice. Now do the same for a list of 25,000,000 items, please. Then you'll realize it's completely useless unless you implement UI virtualization. BTW yes, you can do all that yourself, as I mentioned in my answer, but it doesn't make any sense because you're trying to reinvent the wheel, since all these features are available out of the box in modern technologies, and to be honest, whatever you come up with is going to be inferior to what Microsoft produces, since they know much better than you and me. – Federico Berasategui Oct 13 '14 at 15:27
  • @HighCore If you need to show 25,000,000 items to your user you've got something wrong with your design, I don't need to handle cases that don't happen in my application. – Eduardo Wada Oct 13 '14 at 16:37
  • @EduardoWada fine. Let's see how far you can get with obsolete technology before you hit an impassable wall and realize you need a full rewrite using current, usable technology. Good luck. Goodbye. – Federico Berasategui Oct 13 '14 at 17:33