Skip to content
This repository has been archived by the owner on Sep 18, 2020. It is now read-only.

mvvm framework

Grzegorz Mrukwa edited this page Mar 21, 2017 · 1 revision

MVVM framework

Description

Very simple (almost primitve) framework for development of WPF applications in MVVM design pattern. Contains implementations of Command pattern to bind actions from the view; INotifyPropertyChanged, to simplify automatic updates of views through bindings along with annotation-based validation of fields. Few general-purpose converters are also supplied.

How to use it? (Examples)

View Models

public class MyVm: PropertyChangedNotification //inheriting base Vm class
{
    [Required] // enforces the property to be filled with some characters
    public string SomeStringProperty
    {
        get { return GetValue(() => SomeStringProperty); } // such calls are responsible for validation
        set { SetValue(() => SomeStringProperty, value); } // and notifying view to update displayed values
    }

    private bool _doIWishToExecute = true;

    private RelayCommand _handle;

    public RelayCommand DoSomething
    {
        get
        {
            return _handle ?? (_handle = new RelayCommand(
                execute = () => MessageBox.Show("Done something <lol>."), // any Action
                canExecute = () => _doIWishToExecute // any Predicate or Func<bool>
                ));
        }
    }
}

view

If you set MyVm to be the DataContext, you can do following to allow user enter value for SomeStringProperty:

<TextBox Text="{Binding SomeStringProperty, Mode=TwoWay}" />

or following, to let him call your DoSomething RelayCommand:

<Button Command="{Binding DoSomething}"
        Content="Calls DoSomething RelayCommand" />