Simple WPF MVVM pattern development

Source: Internet
Author: User

    • Model Layer
  Public classSong {Private string_artistname; Private string_songtitle;  Public stringSongTitle {Get{return_songtitle;} Set{_songtitle =value;} }         Public stringArtistname {Get{return_artistname;} Set{_artistname =value;} }    }
    • ViewModel

Relaycommand

  Public classRelaycommand:icommand {Private ReadOnlyFunc<boolean>_canexecute; Private ReadOnlyaction<Object>_execute;  PublicRelaycommand (action<Object>execute): This(Execute,NULL)        {        }         PublicRelaycommand (action<Object> Execute, func<BOOL>CanExecute) {            if(Execute = =NULL)                Throw NewArgumentNullException ("Execute is null"); _canexecute=CanExecute; _execute=Execute; }         Public BOOLCanExecute (Objectparameter) {            return_canexecute = =NULL?true: _canexecute (); }         Public EventEventHandler canexecutechanged {add {if(_canexecute! =NULL) {commandmanager.requerysuggested+=value; }} remove {if(_canexecute! =NULL) {commandmanager.requerysuggested-=value; }            }        }         Public voidExecute (Objectparameter)        {_execute (parameter); }    }
  Public Sealed classsongvm:inotifypropertychanged {PrivateSong _song;  PublicSONGVM () {_song=NewSong () {songtitle ="Ding Ding", Artistname ="WJP" }; }         PublicSong Song {Get{return_song;} Set{_song=value; }        }         Public stringArtistname {Get{returnSong.artistname;} Set            {                if(Artistname! =value) {Song.artistname=value; raisePropertyChanged ("Artistname"); }            }        }         Public stringSongTitle {Get{returnSong.songtitle;} Set            {                if(songtitle!=value) {Song.songtitle=value; raisePropertyChanged ("SongTitle"); }            }        }         Public EventPropertyChangedEventHandler propertychanged; Private voidraisePropertyChanged (stringPropertyName) {PropertyChangedEventHandler handler=propertychanged; if(Handler! =NULL) {Handler ( This,NewPropertyChangedEventArgs (PropertyName)); }        }        Private voidExecute (Objectpara) {            if(Para! =NULL) Artistname=para.        ToString (); }        Private BOOLCanExecute () {return true; }         PublicICommand Updateatistname {Get            {                return NewRelaycommand (Execute, CanExecute); }        }    }
    • View
<window x:class="MVVMLightDemo.View.SampleMVVM"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"XMLNS:LOCALVM="Clr-namespace:mvvmlightdemo.viewmodel"Title="SAMPLEMVVM"height=" $"Width=" $"> <Window.DataContext> <localVM:SongVM/> </Window.DataContext> <Grid> &L T grid.rowdefinitions> <RowDefinition/> <RowDefinition/> <rowdefinition/&        Gt </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <col umndefinition/> </Grid.ColumnDefinitions> <textblock grid.column="0"grid.row="0"text="name"Horizontalalignment="Center"Verticalalignment="Center"/> <textblock grid.column="0"grid.row="1"text="Song name"Horizontalalignment="Center"Verticalalignment="Center"/> <textblock grid.column="1"grid.row="0"text="{Binding Artistname}"Horizontalalignment="Center"Verticalalignment="Center"/> <textblock grid.column="1"grid.row="1"text="{Binding Song.songtitle}"Horizontalalignment="Center"Verticalalignment="Center"/> <button grid.column="0"grid.columnspan="2"grid.row="2"margin="5"Content="Update name"Command="{Binding Updateatistname}"Commandparameter="Wang Junpeng"/> </Grid></Window>

Simple WPF MVVM pattern development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.