问题
So I have been trying to fix many errors in my system for a while and I'm pretty close to being done. One of these errors resulted in me creating a simple list that is the equivalent of my database fields. As we all know, this is is stupid for a lot of data - and now I've finished with test data, it's time to implement a database connection.
So below are the code files that I am using and descriptions with them:
DBDisplayViewModel.cs
public class DBDisplayViewModel
{
public ICollectionView Users { get; private set; }
/// <summary>
/// To pull the database
/// </summary>
private static string connectionString = Properties.Settings.Default.DBUsers;
public DBDisplayViewModel()
{
//TODO Database connection
#Region old code
var _users = new List<User>
{
new User
{
ID = "IDTest",
FirstName = "FNTest",
LastName = "LNTest",
},
#endregion
Users = CollectionViewSource.GetDefaultView(_users);
}
}
Users.cs
public class Patients : INotifyPropertyChanged
{
private string _id;
private string _firstName;
private string _lastName;
/// <summary>
/// Define ID
/// </summary>
public string ID
{
get { return _id; }
set
{
_id = value;
NotifyPropertyChanged("ID");
}
}
/// <summary>
/// Define First Name
/// </summary>
public string FirstName
{
get { return _firstName; }
set
{
_firstName = value;
NotifyPropertyChanged("FirstName");
}
}
/// <summary>
/// Define Last Name
/// </summary>
public string LastName
{
get { return _lastName; }
set
{
_lastName = value;
NotifyPropertyChanged("LastName");
}
}
/// <summary>
/// INotifyPropertyChanged Members
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Private Helpers
/// </summary>
/// <param name="propertyName"></param>
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
DBDisplay.xaml
simply has a binding (that needs to connect to the DB)
<DataGrid ItemsSource="{Binding Users}"
DBDisplay.xaml.cs
simply has the ViewModel in
public DBDisplay()
{
InitializeComponent();
// Pull in the ViewModel
DataContext = new DBDisplayViewModel();
}
So from what I can see, in the View Model, where the #Region
is, I need to connect to my (preexisting) database, DBUsers
Can someone break down the way to do this, and if I'm doing anything wrong? Thank you ever so much
回答1:
If I understand you correctly, the EntityFramework should help you work with the DB. You need to install the NuGet using the EntityFramework. (More info: https://msdn.microsoft.com/en-us/data/jj574514.aspx , http://metanit.com/sharp/entityframework/3.3.php)
app.config
<configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="DictionaryPlus" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=DataBaseName;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
xaml.cs file
public partial class MainWindow : Window
{
Db_Context db = null;
public MainWindow()
{ db = new Db_Context(); }
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
await db.Users.LoadAsync();
/* Data usage */
/* Binding */
CollectionViewSource userViewSource = ((CollectionViewSource)(this.FindResource("userViewSource")));
userViewSource.Source = db.Users.Local;
/* Add data */
db.Users.Add(new User());
await db.SaveChangesAsync();
this.list_Users.Items.Refresh();
}
}
public class Db_Context : DbContext
{
public Db_Context() : base("DataBaseName") { }
public DbSet<User> Users { get; set; }
}
xaml file
<Window.Resources>
<CollectionViewSource x:Key="userViewSource" d:DesignSource="{d:DesignInstance {x:Type Model:User}, CreateList=True}"/>
</Window.Resources>
<ListBox x:Name="list_Users" DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource userViewSource}}" SelectedValuePath="Id" Focusable="False"/>
来源:https://stackoverflow.com/questions/33924821/converting-from-normal-list-data-to-a-database-in-wpf-xaml