Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Wpf mvvm crud
Message
From
27/02/2019 16:21:47
 
 
To
23/02/2019 19:27:40
John Baird
Coatesville, Pennsylvania, United States
General information
Forum:
C#
Category:
Forms
Title:
Miscellaneous
Thread ID:
01666655
Message ID:
01666791
Views:
67
Hi John,


About example in the link:
https://www.codeproject.com/articles/165368/wpf-mvvm-quick-start-tutorial

First case:
In this case, I’ve :
One Model: Song, and two ViewModel class:
public class SongViewModel : ObservableObject and
class AlbumViewModel , and View

Notice: Model,ViewModel and View there are in link https://www.codeproject.com/articles/165368/wpf-mvvm-quick-start-tutorial
(example6)
<Window x:Class="Example6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Example6"
        Title="Example 6" Height="464" Width="530" >
    <Window.DataContext>
        <!-- Declaratively create an instance of our Album View Model-->
        <local:AlbumViewModel />
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Menu Grid.Row="0" Grid.ColumnSpan="3">
            <MenuItem Header="Test">
                <MenuItem Header="Update Artist" Command="{Binding UpdateAlbumArtists}" />
                <MenuItem Header="Add Artist" Command="{Binding AddAlbumArtist}" />
                <MenuItem Header="Update Songs" Command="{Binding UpdateSongTitles}" />
            </MenuItem>
        </Menu>
In this case working Update Artist and Update Songs (Button or menu)


Second case:
I’ve one model and one ViewMode and also working Update Artist and Update Songs (Button or menu)
public class Song : ObservableObject  
    {
        #region Members
        string _artistName;
        string _songTitle;
        #endregion


        #region Properties
        public string ArtistName
        {
 
            get { return _artistName; }  
            set
            {
                if (_artistName != value)
                {
                    _artistName = value;
                    RaisePropertyChanged("ArtistName");
                }
            }
        }

        public string SongTitle
        {
 
            get { return _songTitle; }        
            set
            {
                if (_songTitle != value)
                {
                    _songTitle = value;
                    RaisePropertyChanged("SongTitle");
                }
            }
        }

        #endregion
    }

class AlbumViewModelWithOuthSVM  
    {
        #region Members
        private SongDatabase _database = new SongDatabase();
        ObservableCollection<Song> _songs = new ObservableCollection<Song>();
        int _count = 0;
        #endregion

        #region Properties
        public ObservableCollection<Song> Songs
        {
            get
            {
                return _songs;
            }
            set
            {
                _songs = value;
            }
        }

        private Song _selectedsongs;
        public  Song  SelectedSongs
        {
            get
            {
                return _selectedsongs;
            }
            set
            {
                if (!Equals((_selectedsongs), value))
                {
                    _selectedsongs = value;
                }
            }
        }

        #endregion
        #region Construction
        public AlbumViewModelWithOuthSVM()
        {
            for (int i = 0; i < 3; ++i)
            {
                _songs.Add(new Song { ArtistName = _database.GetRandomArtistName, SongTitle = _database.GetRandomSongTitle });
            }
        }
        #endregion

        #region Commands
        void UpdateAlbumArtistsExecute()
        {
            if (_songs == null)
                return;

            ++_count;
            foreach (var song in _songs)
            {
                song.ArtistName = _database.GetRandomArtistName;
            }
          
        }

        bool CanUpdateAlbumArtistsExecute()
        {
            return true;
        }

        public ICommand UpdateAlbumArtists { get { return new RelayCommand(UpdateAlbumArtistsExecute, CanUpdateAlbumArtistsExecute); } }


        void AddAlbumArtistExecute()
        {
            if (_songs == null)
                return;

            _songs.Add(new Song { ArtistName = _database.GetRandomArtistName, SongTitle = _database.GetRandomSongTitle });

          
        }

        bool CanAddAlbumArtistExecute()
        {
            return true;
        }

        public ICommand AddAlbumArtist { get { return new RelayCommand(AddAlbumArtistExecute, CanAddAlbumArtistExecute); } }

        void UpdateSongTitlesExecute()
        {
            if (_songs == null)
                return;

            ++_count;
            foreach (var song in _songs)
            {
                song.SongTitle = _database.GetRandomSongTitle;
            }
        }

        bool CanUpdateSongTitlesExecute()
        {
            return true;
        }

        public ICommand UpdateSongTitles { get { return new RelayCommand(UpdateSongTitlesExecute, CanUpdateSongTitlesExecute); } }

        #endregion
    }

<Window x:Class="Example6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Example6"
        Title="Example 6" Height="464" Width="530" >
    <Window.DataContext>
        <!-- Declaratively create an instance of our Album View Model-->
        <local:AlbumViewModelWithOuthSVM />
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Menu Grid.Row="0" Grid.ColumnSpan="3">
            <MenuItem Header="Test">
                <MenuItem Header="Update Artist" Command="{Binding UpdateAlbumArtists}" />
                <MenuItem Header="Add Artist" Command="{Binding AddAlbumArtist}" />
                <MenuItem Header="Update Songs" Command="{Binding UpdateSongTitles}" />
            </MenuItem>
        </Menu>
        <Label Grid.Column="0" Grid.Row="1" Content="Example 6 - collection" />

        <StackPanel Grid.Column=" 0" Grid.Row=" 2">
            <Label Content="{Binding  SelectedSongs.ArtistName  }"/>
            <Label Content="{Binding  SelectedSongs.SongTitle  }"/>
        </StackPanel>
        
        <Button Grid.Column="1" Grid.Row="1" Content="Add new artist" Command="{Binding AddAlbumArtist}" />
        <ListView  Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2" 
                  ItemsSource="{Binding Songs}" 
                  SelectedItem="{Binding SelectedSongs, Mode=TwoWay}"   >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Label Content="{Binding ArtistName}" />
                        <Label Content="{Binding SongTitle}" FontSize="10" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button Grid.Column="2" Grid.Row="1" Content="Update Artist Name" Command="{Binding UpdateAlbumArtists}" />
        <Button Grid.Column="3" Grid.Row="1" Content="Update Song Titles" Command="{Binding UpdateSongTitles}" />
    </Grid>
</Window>
My question is:

Why in the second case, if I want to use one model and one VievModel, I have to use the RaisePropertiChanged ("...") in the model, while in the first case I have must to use two ViewMode?

Should not there be in this case one model and one VM, and RaisePropertyChanged("...") to use Only in ViewModel ?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform