10

I have a dialog that pops up over the main screen (it's actually a user control that appears on the page as per the application demo from Billy Hollis) in my application that has data from the main screen to be edited. The main screen is read only.

The problem I have is that when I change the data in the dialog, the data on the main screen updates as well. Clearly they are bound to the same object, but is there a way to stop the binding update until I click save in my dialog?

  • The article [Edit With Explicit UpdateSourceTrigger](http://kvelikov.wordpress.com/2013/07/30/edit-with-explicit-updatesourcetrigger/) will give you more information about how to implement Save and Cancel with `UpdateSourceTrigger=Explicit`. – Камен Великов Jul 30 '13 at 20:59
  • They only way I've seen it done is how Josh Smith mentions [here](http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a38b812e-f7c3-4f5b-bb90-c3bfdb530817) with converters. Not the easiest method though. – Crippeoblade May 27 '09 at 08:28
  • Related: https://stackoverflow.com/questions/1091036/how-to-cancel-an-edit-to-an-object-using-mvvm/61599556#61599556 – StayOnTarget May 04 '20 at 19:05

3 Answers3

10

You could use a BindingGroup :

...
<StackPanel Name="panel">
    <StackPanel.BindingGroup>
        <BindingGroup Name="bindingGroup"/>
    </StackPanel.BindingGroup>
    <TextBox Text="{Binding Foo}"/>
    <TextBox Text="{Binding Bar}"/>
    <Button Name="btnSubmit" Content="Submit" OnClick="btnSubmit_Click"/>
    <Button Name="btnCancel" Content="Cancel" OnClick="btnCancel_Click"/>
</StackPanel>
...

Code behind :

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    panel.BindingGroup.BeginEdit();
}

private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
    panel.BindingGroup.CommitEdit();
    panel.BindingGroup.BeginEdit();
}

private void btnCancel_Click(object sender, RoutedEventArgs e)
{
    panel.BindingGroup.CancelEdit();
    panel.BindingGroup.BeginEdit();
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Great idea! I only got this working if I declared the group in each binding statement. This is however, much easier than the other alternatives. – Telavian Sep 30 '10 at 18:15
3

Have a look at the Binding.UpdateSourceTrigger property.

You can set the Binding in your dialog like so

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

And then call the UpdateSource method in your button save event

myTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();

Once you've called UpdateSource the source object will be updated with the value from the TextBox

Ray
  • 45,695
  • 27
  • 126
  • 169
  • I tried this but it doesn't work. It still updates the TextBlock on the main screen. The Explicit option does state that it will only update when the UpdateSource method is called, but not in this case. –  May 28 '09 at 04:12
  • Really? I'm surprised. I just tried it and it works for me. Maybe there's something else in your application that causing problems? – Ray May 28 '09 at 20:27
  • It definitely works but you should iterate through all the bindings in order to call UpdateSource() on each. The simplest way to do it is to associate the bindings to one BindingGroup. As a bonus you get UpdateSourceTrigger set to Explicit on the associated bindings implicitly. – Ilya Serbis Nov 24 '12 at 16:50
0

I also choose to use BindingGroup. But instead of BeginEdit() / CommitEdit() / CancelEdit() pattern I call UpdateSource() explicitly on all the bindings associated with BindingGroup. This approach allows me to add only one event handler instead of 3.

private void OkButton_Click(object sender, RoutedEventArgs e)
{
    CommitChanges();
    DialogResult = true;
    Close();
}

private void CommitChanges()
{
    foreach (var bindingExpression in this.BindingGroup.BindingExpressions)
    {
        bindingExpression.UpdateSource();
    }
}
Ilya Serbis
  • 21,149
  • 6
  • 87
  • 74