Robert.S

My continued endeavor to understand WPF Bindings (yay), with some issues (boo), and the chance to get more points for you (yay).

Background:
Listview with a DataContext set to an ObservableCollection of Foos.
A User Control for editing items in the list that accepts a DataContext of Foo.

Step 1
The List displays, I click the Create New button, the edit control is show, the fields are entered, validation hotness ensues, the user presses OK, the new item is persisted to the db, the ObservableCollection of Foos has the processed item added to it which displays in the list. (sweet).

Step 2
The List displays, I select an item in the ListView, the event for the selection changed is handled, I retrieve the Foo from the item, and I set the DataContext of the edit control to the Foo. (so far so good).

What I ran into is the change to the TextBox value is immediately updated to the ListView when the control looses focus. I get this is the default behavior (LostFocus) for a Binding UpdateSourceTrigger with a Mode of TwoWay.

On a suggestion at the tail end of another one of my threads (making progress) I changed the Trigger to Explicit, which will require me to call UpdateSource on each DataBinding in to the DataContext. This is an ok solution, however the value of the TextBox is not set initially from the DataContext. I attempted to UpdateTarget (would make sense to me) on the TextBox's Binding once the DataContext is set, but it did not set the value. (boo)

What would make more sense to me (from my little understanding of WPF) is setting the Binding Mode on the TextBox to OneWay and then calling Update Source once validation, business logic proccessing, and data persistance occurs.

What I would like to accomplish in short:
List -> Edit Item -> automatically fill element based on binding -> modify with constraints of validation and middleware business logic processing on OK button click -> update ObservableCollection with the modified Foo and have it displayed in the list. (woot)

Thank you for reading all of this, and thank you for any help.


Re: Windows Presentation Foundation (WPF) Updating DataContext Part Deux

Dr. WPF

If I'm understanding your scenario correctly, things should be working exactly how you're hoping they will.

If you set a TwoWay binding on the Text property of the TextBox with UpdateSourceTrigger=Explicit, you should definitely see the initial Text value populated when you set the DataContext. Specifically, the target should be updated immediately whenever the source changes (or when the source is first set). It is only the source that requires an explicit update.

Can you stick a dummy converter in your binding and see what value is being passed through the converter when you update the DataContext You can use the following and just put a breakpoint in the Convert function. This may point you in the right direction for identifying the problem.

Code Snippet

public class DummyConverter : IValueConverter

{

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

{

return value;

}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

{

return value;

}

}






Re: Windows Presentation Foundation (WPF) Updating DataContext Part Deux

Robert.S

I see what it was and it works.

Now just to work the Validation which calls update source to check values.

Thanks