首页 > Windows开发 > 详细

简易的WPF MVVM模式开发

时间:2016-02-24 09:29:40      阅读:307      评论:0      收藏:0      [点我收藏+]
  • 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>

 

简易的WPF MVVM模式开发

原文:http://www.cnblogs.com/smartsensor/p/5211794.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!