> ><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)
>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> > >>