码迷,mamicode.com
首页 > Windows程序 > 详细

简易的WPF MVVM模式开发

时间:2016-02-24 09:31:00      阅读:377      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!