1

I Am trying to learn WPF and trying to figure out the best way to bind my LINQ to SQL classes to a WPF window.

so far I have this:

using (Database.DataClasses1DataContext db = new Database.DataClasses1DataContext())
{
    var user = db.Users.Where(u => u.Email == TextboxEmail.Text).FirstOrDefault();
    CustomerTab.DataContext = user;
}

and in XAML

<TextBox Name="TextboxContactName" Grid.Row="1" Margin="0,2" Grid.Column="1" Text="{Binding Path=ContactName, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"></TextBox>

So this provides a One-Way Bind that displays the name to the textbox.

I have been trying to follow examples online, but my question is, how do I make it update the LINQ Automatically when textbox loses focus.

Do I need to manually enter it in the lost focus event?

edit I have tried to run this when the textbox loses focus:

   using (Database.DataClasses1DataContext db = new Database.DataClasses1DataContext())
    {
        Database.User customer = (Database.User)CustomerTab.DataContext;

        db.SubmitChanges();
    }

but the Customer Object has no updates to it. I know I could do:

customer.ContactName = textbox.Text;
db.submitChanges();

But I thought WPF was suppose to handle this for me?

2
  • if I understand you want to update the view (textbox) from view model (where your linq code is) - when view changes / textbox loses focus? and if so why Commented Apr 22, 2012 at 20:52
  • I want to have 2 arrows to browse through customers.. When you find a customer you want to edit, you can just edit the name in the textbox then when it loses focus, it saves those changes to the database.. Commented Apr 22, 2012 at 21:00

2 Answers 2

1

you should at least add a layer of abstraction between the UI and the Data layer. In order to get the data persisted you need to save the context, not just to update the property in the user object. it's not a best practice anyway, and try to think about the fact that everytime the textbox lost focus there is a trip to the database. anyway adding an abstraction layer, and saving the context in the setter of the property should work for you.

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

1 Comment

It's not a real project, only for learning how WPF databinds work.
0

I have little experience with binding, but try this in your XAML:

<TextBox Name="TextboxContactName" Grid.Row="1" Margin="0,2" Grid.Column="1" Text="{Binding Path=ContactName, UpdateSourceTrigger=LostFocus}"></TextBox>

Edit 1:

Fine print - "The UpdateSourceTrigger property specifies when to update the source with changes. This is valid only with Mode=TwoWay bindings (which is the default)."

Edit 2:

If this doesn't work, you may need to explicitly call SubmitChanges() on the DataContext.

Comments

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.