Good afternoon everyone,
Basically I am creating a program to store a vinyl collection.
The project was originally just a WPF application, then I had to make the changes to implement it using MVVM pattern, and then replace the old school methods of using SQL, to use EF instead.
However, I can get my data and view it in a listview using EF. The problem seems to be when I edit an entry, my changes occur when I click update, but the changes are not sent to SQL and saved.
Just to clarify, my project needs to be in MVVM pattern, using entity framework, code first to deal with the sql data, and it has to be in c# using WPF.
Any help or advice would be greatly appreciated. I have had no response so far, but many people viewing this. If there is anyone out there that can point me in the right direction, I would really appreciate it, thanks.
Below is my code:
View - Xaml
<UserControl x:Class="VinylBox.View.ucVinyl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="485" d:DesignWidth="525">
<Grid Margin="0,0,0,20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListView Name="VinylGrid" Grid.Row="1" Margin="4,199,12,24" ItemsSource="{Binding Vinyls, Mode=TwoWay}" >
<ListView.View>
<GridView x:Name="grdVinyl">
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name, Mode=TwoWay}" Width="120"/>
<GridViewColumn Header="VinylLabel" DisplayMemberBinding="{Binding VinylLabel, Mode=TwoWay}" Width="70"/>
<GridViewColumn Header="Genre" DisplayMemberBinding="{Binding Genre, Mode=TwoWay}" Width="60"/>
<GridViewColumn Header="SubGenre" DisplayMemberBinding="{Binding SubGenre, Mode=TwoWay}" Width="60"/>
<GridViewColumn Header="Photo" DisplayMemberBinding="{Binding Photo, Mode=TwoWay}" Width="40"/>
<GridViewColumn Header="Url" DisplayMemberBinding="{Binding Url, Mode=TwoWay}" Width="200"/>
</GridView>
</ListView.View>
</ListView>
<TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left"
Margin="80,7,0,0" Name="txtName" VerticalAlignment="Top" Width="178"
Text="{Binding ElementName=VinylGrid, Path=SelectedItem.Name, Mode=TwoWay}" />
<TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left"
Margin="80,35,0,0" Name="txtVinylLabel" VerticalAlignment="Top" Width="178"
Text="{Binding ElementName=VinylGrid, Path=SelectedItem.VinylLabel, Mode=TwoWay}" />
<TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left"
Margin="80,62,0,0" Name="txtGenre" VerticalAlignment="Top" Width="178"
Text="{Binding ElementName=VinylGrid, Path=SelectedItem.Genre, Mode=TwoWay}" />
<TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left"
Margin="80,90,0,0" Name="txtSubGenre" VerticalAlignment="Top" Width="178"
Text="{Binding ElementName=VinylGrid, Path=SelectedItem.SubGenre, Mode=TwoWay}" />
<TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left"
Margin="80,150,0,0" Name="txtUrl" VerticalAlignment="Top" Width="279"
Text="{Binding ElementName=VinylGrid, Path=SelectedItem.Url, Mode=TwoWay}" />
<Label Content="Name" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="10,10,0,397" Name="lblName"/>
<Label Content="VinylLabel" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="10,35,0,372" Name="lblVinylLabel"/>
<Label Content="Genre" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="10,62,0,345" Name="lblGenre"/>
<Label Content="SubGenre" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="10,90,0,317" Name="lblSubGenre"/>
<Label Content="Photo" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="263,10,0,397" Name="lblPhoto"/>
<Label Content="Url" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="10,150,0,257" Name="lblUrl"/>
<Image Name="imgPhoto" Source="{Binding ElementName=VinylGrid, Path=SelectedItem.Photo, Mode=TwoWay}" HorizontalAlignment="Left" Height="110"
Margin="263,35,0,0" Grid.RowSpan="2" VerticalAlignment="Top" Width="242"/>
<Button Content="Update" Grid.Row="1" HorizontalAlignment="Left" Margin="366,150,0,255" Name="btnUpdate" Width="141" Command="{Binding Path=UpdateCommand}" VerticalAlignment="Center" Height="30"/>
</Grid>
</UserControl>
View - Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VinylBox;
using VinylBox.ViewModel;
using VinylBox.Model;
using System.Collections.ObjectModel;
namespace VinylBox.View
{
public partial class ucVinyl : UserControl
{
public ucVinyl()
{
InitializeComponent();
VinylViewModel vinylViewModel = new VinylViewModel();
this.DataContext = vinylViewModel;
}
}
}
Model - Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
namespace VinylBox.Model
{
public class Vinyl : INotifyPropertyChanged
{
private string name;
private string vinylLabel;
private string genre;
private string subGenre;
private byte[] photo;
private string url;
public int VinylId { get; set; }
public string Name
{
get
{
return name;
}
set
{
name = value; OnPropertyChanged("Name");
}
}
public string VinylLabel
{
get
{
return vinylLabel;
}
set
{
vinylLabel = value; OnPropertyChanged("VinylLabel");
}
}
public string Genre
{
get
{
return genre;
}
set
{
genre = value; OnPropertyChanged("Genre");
}
}
public string SubGenre
{
get
{
return subGenre;
}
set
{
subGenre = value; OnPropertyChanged("SubGenre");
}
}
public byte[] Photo
{
get
{
return photo;
}
set
{
photo = value; OnPropertyChanged("Photo");
}
}
public string Url
{
get
{
return url;
}
set
{
url = value; OnPropertyChanged("Url");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
View Model - Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.ComponentModel;
using System.Data.Entity;
using VinylBox.Model;
using VinylBox;
using System.Collections.ObjectModel;
namespace VinylBox.ViewModel
{
class VinylViewModel
{
private IList<Vinyl> VinylList;
private byte[] photo;
ObservableCollection<Vinyl> obVinyl = new ObservableCollection<Vinyl>();
public VinylViewModel()
{
using (var db = new VinylContext())
{
var query = from b in db.VinylSet
orderby b.VinylId
select b;
foreach (var item in query)
{
obVinyl.Add(item);
VinylList = obVinyl;
}
}
}
public IList<Vinyl>Vinyls
{
get { return VinylList; }
set { VinylList = value; }
}
public void InsertVinyl(int vinylId, string name, string vinylLabel, string genre, string subGenre, string url)
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<VinylContext>());
VinylContext contextVinyl = new VinylContext();
Vinyl vinyl = new Vinyl();
using (var vinylContext = new VinylContext())
{
var query = from b in vinylContext.VinylSet
orderby b.VinylId
select b;
foreach (var item in query.Where(v => v.VinylId == vinylId))
{
if (vinylId == item.VinylId)
{
vinyl.Name = item.Name;
vinyl.VinylLabel = item.VinylLabel;
vinyl.Genre = item.Genre;
vinyl.SubGenre = item.SubGenre;
vinyl.Photo = item.Photo;
vinyl.Url = item.Url;
}
else
{
vinyl.Name = name;
vinyl.VinylLabel = vinylLabel;
vinyl.Genre = genre;
vinyl.SubGenre = subGenre;
vinyl.Url = url;
contextVinyl.VinylSet.Add(vinyl);
contextVinyl.SaveChanges();
}
}
}
}
private ICommand mUpdater;
public ICommand UpdateCommand
{
get
{
if (mUpdater == null)
mUpdater = new Updater();
return mUpdater;
}
set
{
mUpdater = value;
}
}
private class Updater : ICommand
{
#region ICommand Members
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
}
#endregion
}
}
}
Data Context - Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using VinylBox.Model;
namespace VinylBox.ViewModel
{
public class VinylContext : DbContext
{
public DbSet<Vinyl> VinylSet { get; set; }
}
}
I would really appreciate it if someone can point me to a tutorial/example that shares what I am trying to achieve. Or providing any kind of advice, would be appreciated.
Kind Regards
Dean