Model / View Programing - WPF and MySQL

3

I had already done some C ++ programs using the QT framework and its Model / View Programming ( link ).

The concept was simple:

  • created a template accessing MySQL
  • Creating the view that accesses the template ...

Now that I'm using C # and WPF I'm a little lost. What tools do I now have at my disposal to implement this kind of solution?

A simple example would help.

    
asked by anonymous 21.10.2016 / 00:03

1 answer

2

The most common pattern in WPF is MVVM (Model-View-ViewModel) which is a default of type MV* .

In this pattern there are 3 components. The view (which has a set of controls arranged in a certain way), the model (which has the data with logic) and the ViewModel (which has the information to be presented in the view, as well as the events of the controls, such as buttons).

InMVVMitistypicalforcontrolstosupportdata-binding.Thisisjusttosaythataparticularcontrolpropertyisboundtoaclassproperty.ForexamplethetextinatextboxmaybelinkedtoaNomeproperty.Bindingsalsorefreshtheviewautomaticallyaslongastheynotifytheview.Tonotifytheviewitisnecessary,inWPF,toimplementtheinterfaceINotififyPropertyChangedandtoinvoketheeventPropertyChanged

publicclassModel:INotifyPropertyChanged{publiceventPropertyChangedEventHandlerPropertyChanged;[NotifyPropertyChangedInvocator]protectedvirtualvoidOnPropertyChanged([CallerMemberName]stringpropertyName=null){PropertyChanged?.Invoke(this,newPropertyChangedEventArgs(propertyName));}protectedboolSetField<T>(refTfield,Tvalue,[CallerMemberName]stringpropertyName=null){if(!field.Equals(value)){field=value;OnPropertyChanged(propertyName);returntrue;}returnfalse;}}

TheSetFieldmethodisasimplewaytoinvoketheevent.Itonlyinvokesifthevaluechanges.YourViewsModelscannowderivefromthisclass.

publicclassPessoaViewModel:Model{privatestring_nome;publicstringNome{get{return_nome;}set{SetField(ref_nome,value);}}}

AsIexplainedbefore,thecommandscanalsobebinded.CommandsshouldimplementtheICommandinterface.Inordertoavoidcreatingaclassforeachtypeofcommand,itiscommontocreateonethatexecutesanyAction,passedintheclassconstructor:

publicclassDelegatedCommand:ICommand{protectedreadonlyAction<object>_action;protectedPredicate<object>_canExecute;publicDelegatedCommand(Action<object>action,Predicate<object>canExecute=null){_action=action;_canExecute=canExecute;}publicboolCanExecute(objectparameter){return_canExecute?.Invoke(parameter)??true;}publicvoidExecute(objectparameter){_action(parameter);}publiceventEventHandlerCanExecuteChanged{add{CommandManager.RequerySuggested+=value;}remove{CommandManager.RequerySuggested-=value;}}}

YoucannowaddcommandstoyourViewModel.Thereareseveralwaystodothis,butI'llshowyoutheoneIusuallyuse:

publicclassPessoaViewModel:Model{privateICommand_command;publicICommandCommand{get{if(_command!=null)return_command;return_command=newDelegtedCommand((arg)=>{Console.WriteLine("O código do comando vai aqui");
            });
        }
    }

}

Of course the commands can change the state of the properties, if they invoke PropertyChanged then the interface will be updated automatically.

Finally a fairly simple view (omitted some attributes of Window):

<Window>   
    <Window.DataContext>
        <local:PessoaViewModel />
    </Window.DataContext>
    <StackPanel>
        <TextBox Text="{Binding Nome}" />
        <Button Command="{Binding Command}" />
    </Stackpanel>
</Window>

References

What is MVP and MVVM

    
21.10.2016 / 01:08