- Model层
public class Song { private string _artistName; private string _songTitle; public string SongTitle { get { return _songTitle; } set { _songTitle = value; } } public string ArtistName { get { return _artistName; } set { _artistName = value; } } }
- ViewModel
RelayCommand
public class RelayCommand : ICommand { private readonly Func<Boolean> _canExecute; private readonly Action<object> _execute; public RelayCommand(Action<object> execute) : this(execute, null) { } public RelayCommand(Action<object> execute, Func<bool> canExecute) { if (execute == null) throw new ArgumentNullException("execute is null"); _canExecute = canExecute; _execute = execute; } public bool CanExecute(object parameter) { return _canExecute == null ? true : _canExecute(); } public event EventHandler CanExecuteChanged { add { if (_canExecute != null) { CommandManager.RequerySuggested += value; } } remove { if (_canExecute != null) { CommandManager.RequerySuggested -= value; } } } public void Execute(object parameter) { _execute(parameter); } }
public sealed class SongVM : INotifyPropertyChanged { private Song _song; public SongVM() { _song = new Song() { SongTitle = "叮叮当", ArtistName = "wjp" }; } public Song Song { get { return _song; } set { _song = value; } } public string ArtistName { get { return Song.ArtistName; } set { if (ArtistName != value) { Song.ArtistName = value; RaisePropertyChanged("ArtistName"); } } } public string SongTitle { get { return Song.SongTitle; } set { if (SongTitle!=value) { Song.SongTitle = value; RaisePropertyChanged("SongTitle"); } } } public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } private void Execute(object para) { if (para != null) ArtistName = para.ToString(); } private bool CanExeCute() { return true; } public ICommand UpdateAtistName { get { return new RelayCommand(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="200" Width="200"> <Window.DataContext> <localVM:SongVM/> </Window.DataContext> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Grid.Row="0" Text="姓名" HorizontalAlignment="Center" VerticalAlignment="Center"/> <TextBlock Grid.Column="0" Grid.Row="1" Text="歌曲名称" 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="更新姓名" Command="{Binding UpdateAtistName}" CommandParameter="王俊鹏"/> </Grid> </Window>
时间: 2024-11-02 16:24:21