Differences between revisions 4 and 6 (spanning 2 versions)
Revision 4 as of 2013-02-15 18:31:01
Size: 1469
Editor: hsc-129
Comment:
Revision 6 as of 2013-02-15 18:40:12
Size: 3075
Editor: embsys-tekscope-2
Comment:
Deletions are marked like this. Additions are marked like this.
Line 29: Line 29:
== WPF Part == == WPF Part (1) ==
Line 34: Line 34:
  1. Add two Label and two TextBox objects on the screen and one DataGrid so that it looks like the following:   1. Add two Label and two TextBox objects on the screen, one DataGrid and three buttons so that it looks like the following ('''Don't worry about the Binding yet though!'''):

{{attachment:XAML_View.png}}


== Creating the View Model ==

There are two parts to the view model. First you need to create an object that implements ICommand. I'm using a rather standard way of doing these called a DelegateCommand

{{{!csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace EF_WPF_Example
{
    class DelegateCommand : ICommand
    {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;
        private bool flagExecutable = false;

        public event EventHandler CanExecuteChanged;

        public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            bool flag = _canExecute == null || _canExecute(parameter);
            if (flagExecutable != flag)
            {
                flagExecutable = !flagExecutable;
                RaiseCanExecuteChanged();
            }
            return flag;
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }
    }
}

}}}

Entity Framework and Windows Presentation Foundation Walk Through

Entity Framework

The first thing to do is get the Database Model going:

  1. Fire up Visual Studio 2012
  2. Create a new project called EF_WPF_Example, Select Windows, WPF project type.
  3. If the Data Sources side bar (windows) is not visible Press Shift+Alt+D
  4. Click Add New Data Sources and in the wizard...
    1. Click Database, Next
    2. Entity Data Model, Next
    3. Generate from database, Next
    4. Click New Connection...
    5. Give your server name and click on the drop down under "Select or enter a database name"
    6. Select the University Database Example that we have been working on.
    7. Test the connection to make sure that it works, then click OK
    8. Make a special note of the connection string name, Click next
    9. Under tables place check marks next to "advisor", "instructor", "student" and notice the Model Namespace (Mine was UniversityExampleModel), Click Finish.

Your file should now look like this:

EFModel.png

One last thing: Rename the file to be UniversityModel (I just don't like Model1.edmx).


WPF Part (1)

Now we will build the GUI (You could do this in Blend too, but I'm going to use Visual Studio 2012).

  1. Make sure the toolbox is visible.
  2. Add two Label and two TextBox objects on the screen, one DataGrid and three buttons so that it looks like the following (Don't worry about the Binding yet though!):

XAML_View.png

Creating the View Model

There are two parts to the view model. First you need to create an object that implements ICommand. I'm using a rather standard way of doing these called a DelegateCommand

{{{!csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input;

namespace EF_WPF_Example {

}

}}}

DatabaseManagementSystems/EntityFrameworkWpfExample (last edited 2013-02-15 18:58:39 by embsys-tekscope-2)