Massimo A. Santin

I have a XAML page with many fields bounded to the fields of a typed DataRow (PersonDataSet.PersonRow). This PersonRow is generated by Visual Studio and have many fields like Name, Phone, Mobile, Address, SSN, etc.

In the page every field is defined with some XAML code similar to this snippet:

<TextBox Name="Name" Text="{Binding Path=Name, UpdateSourceTrigger=Explicit}"></TextBox>

I have different types of controls like TextBox or ComboBox. I used explicit update because I want to save modifications only when the user press a Save button. In the code I defined

PersonDataSet.PersonRow personRow;

and I used this variable as page DataContext object. The variable references to an instance contained in a PersonDataSet object (result of a query on a SQL Server db).

Basically I have two event handler: SaveData e UndoData. The first is called when the user clicks a save button, the second when the user clicks an undo button.

public void SaveData(object sendere, RoutedEventArgs args)

{

BindingExpression exprName = Name.GetBindingExpression(TextBox.TextProperty);

exprName.UpdateSource();

¡­

¡­ many lines of code similar to the two lines above

¡­

// save the data into the db.

using (PersonDataSetTableAdapters.PersonTableAdapter ta =

new PersonDataSetTableAdapters.PersonTableAdapter ())

{

ta.Update(personRow);

}

}

}

public void UndoData(object sendere, RoutedEventArgs args)

{

BindingExpression exprName = Name.GetBindingExpression(TextBox.TextProperty);

exprName.UpdateTarget();

¡­

¡­ many lines of code similar to the two lines above

¡­

}

The code is working well but if I have 50 fields I need to write 200 lines of code. I can use some scripting or macros but I'm worried if there is a more efficient way to have the same behavior.

Any suggestions or ideas




Re: Windows Presentation Foundation (WPF) How to manage explicit updating of many bounded controls?

lee d

I think you can loop through the children and depending on the type of control get the bindingexpression and call updatesource




Re: Windows Presentation Foundation (WPF) How to manage explicit updating of many bounded controls?

Massimo A. Santin

After your suggestion I created a code similar to this:

public void RecurseUndoData(UIElement element)

{

if (element is Panel)

{

foreach (UIElement childElement in ((Panel)element).Children)

{

RecurseUndoData(childElement);

}

}

else

{

// you need to know which type of controls and which dependency property to use

BindingExpression bindExpr = null;

if (element is TextBox)

{

bindExpr = ((TextBox)element)

.GetBindingExpression(TextBox.TextProperty);

}

else if (element is ComboBox)

{

bindExpr = ((ComboBox)element)

.GetBindingExpression(ComboBox.SelectedValueProperty);

}

else if (element is DateControls.DatePicker)

{

bindExpr = ((DateControls.DatePicker)element)

.GetBindingExpression(DateControls.DatePicker.ValueProperty);

}

if (bindExpr != null)

{

bindExpr.UpdateTarget();

}

}

}

This code navigates the tree and updates the target (or the source if you change UpdateTarget with UpdateSource). Using this the function UndoData becomes

public void UndoData(object sendere, RoutedEventArgs args)
{
// Grid1 is the panel (a grid) that contains controls
RecurseUndoData(Grid1);
}

I need to understand if the code is able to run with every control. Eventually I can put the code in an helper class and reuse it every time I need. The problem with this solution is that you need to know the type of the controls and the DependencyProperty to which you bounded the data. I would prefere a more generic solution.

What do you think






Re: Windows Presentation Foundation (WPF) How to manage explicit updating of many bounded controls?

lee d

yes, you are right. you have to know those before hand. I am not sure of a generic solution