15

I know there are patterns like MVC to separate view from logic, however, I don't know how common they are in Winform applications.

For a C# Winform application, I may start with a Form and gradually add UI components to it, then for the events of components, (click, textchanged...) I call my functions, or directly write my logic there!

I know that is a bad habit, but I don't know what is the best way to start such a project in Visual Studio (a template, a framework, starting point), Does MVC the only solution? Should I do it for any project?!

I would like to receive some guidelines or lightweight framework to get started.

23

MVVM(Model-View-ViewModel) Pattern can be used in the Winforms

Model

public class Person
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
}

ViewModel

public class PersonViewModel : INotifyPropertyChanged
{
    private Person _Model;

    public string FirstName
    {
        get { return _Model.FirstName; }
        set(string value)
        {
            _Model.FirstName = value;
            this.NotifyPropertyChanged("FirstName");
            this.NotifyPropertyChanged("FullName"); //Inform View about value changed
        }
    }

    public string LastName
    {
        get { return _Model.LastName; }
        set(string value)
        {
            _Model.LastName = value;
            this.NotifyPropertyChanged("LastName");
            this.NotifyPropertyChanged("FullName");
        }
    }

    //ViewModel can contain property which serves view
    //For example: FullName not necessary in the Model  
    public String FullName
    {
        get { return _Model.FirstName + " " +  _Model.LastName; }
    }

    //Implementing INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

View

public class PersonView: Form
{
    //Add two textbox and one label to the form
    //Add BindingSource control which will handle 
    //ViewModel and Views controls changes


    //As viewmodel you can use any type which of course have same named properties
    public PersonView(Object viewmodel)
    {
        this.InitializeComponents();

        this.ViewModelBindingSource.DataSource = viewmodel;
        this.InitializeDataBindings();
    }

    private void InitializeDataBindings()
    {
        this.TextBoxFirstName.DataBindings.Add("Text", this.ViewModelBindingSource, "FirstName", true);
        this.TextBoxLastName.DataBindings.Add("Text", this.ViewModelBindingSource, "LastName", true);
        this.LabelFullName.DataBindings.Add("Text", this.ViewModelBindingSource, "FullName", true);
    }
}

Read more about databinding in Winforms from MSDN

0

Obviously WinForms does not natively support one design pattern over another - the one that might not work is MVVM because you cannot "bind" data to the view model and have it update the data directly.

Otherwise - I would attempt WinForms with MVP - I've seen that done before - here's a link to look at https://winformsmvp.codeplex.com/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.