Public classSong {Private string_artistname; Private string_songtitle; Public stringSongTitle {Get{return_songtitle;} Set{_songtitle =value;} } Public stringArtistname {Get{return_artistname;} Set{_artistname =value;} } }
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); } } }
<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