1

I want to insert data from a form Data Base (used with entity Framwork), until the day I used the manual way as below.

var member = new MorEntities();
Person one_person = new Person
{
   First_Name=txt_F_N.Text,
   Family_Name = txt_L_N.Text,
}
member.People.AddObject(one_person);
member.SaveChanges();

To the best understood in WPF, there is a way in the XAML (binding), I checked online and could not understand, can anyone give a small example from which I will continue

1 Answer 1

1

You can uses so called Properties with public getters and setters and bind that to the textbox

public class MainViewModel : NotificationObject
{
    public MainViewModel()
    {
        Person = new Person();
        SaveCommand = new DelegateCommand(SaveExecuted);
    }

    // Properties
    public Person Person { get; set; }

    // Commands
    public ICommand SaveCommand { get; set; }
    private void SaveExecuted()
    {
        // Do some save logic here!
    }

}

public class Person
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public int Age { get; set; }
}

And in your textbox XAML

<TextBox Text="{Binding Person.FirstName}" />
<Button Command="{Binding SaveCommand}" Content="Save"/>

And in the codebehind add this line

public MainWindow()
{
   InitializeComponent();
   DataContext = new MainViewModel(); //Add this line
}

something like that dont forget to set the DataContext of the View to itself or another public object you want to use the properties of.

also take a look at MVVM nice way to use WPF and use separation of conserns

Make a button on you View called Save and Band this button to a Command like you do with the other properties And in the Execute method save to the Database or update.

the NotificationObject and DelegateCommand are from Prism 4

Sign up to request clarification or add additional context in comments.

5 Comments

Why does it seem the simplest way better than mine (it's more complicated)
Well I'm not saying this is the simplest way, your not asking for the simplest way. You were looking for a small sample from which you could start. I think this is a nice way to start. And if you don't understand just ask.
First thanks for the reply and the help ,To this day I have not touched in MVVM, where to start? What to download?
If you click on the link MVVM of my first post you can see some information about MVVM. You don't have to install anything to use MVVM, there are however a couple of libs that will help you using it. Like Prism but a class implementing INotifyPropertyChanged to have one-way and two-way bindings is enough.
I still think this is the answer to your question maybe you can accept it answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.