DataContext Binding and Refresh


Question

My scenario is:

I have a WPF Window with 3 data-bound text boxes

SettingsUI : Window

<Grid Name="SettingsUIGrid1">
    <TextBox Text="{Binding val1}" ....
    <TextBox Text="{Binding val2}" ....
    <TextBox Text="{Binding val3}" ....
</Grid>

In the constructor I do this:

SettingsUIGrid1.DataContext = coll[0]; // collection first value

When the Cancel button is clicked, I close my window:

private void btnCancel_Click(object sender, RoutedEventArgs e) {
Close();
}

When I click the Show button, is shows values from the DB in text boxes, if user changes a text box value, and reloads the window the new value is displayed not the old one. Can someone suggest what to do to reload the values again and clear the in memory object?

1
3
1/11/2019 10:14:24 AM

The Binding works two way: it takes the value and sets it editable in the textbox, and if the value changes, it updates the original object's value...

If you only wish to show the data, you can use the Mode=OneTime option in the Binding

{Binding Path =val1, Mode=OneTime}

This will only evaluate the data once..

If you do need to modify the data, make sure you clone or use another object for your datasource property.. This way you can discard the datasource object if it is no longer needed...

HTH

4
11/19/2008 1:05:36 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon