views:

505

answers:

1

DataForms seem to update their CurrentItem as soon as the user tabs out of a field. This happens even when AutoCommit = false. The side effect of that behavior is that other controls that are bound to the data update while the user edits data instead of when the user clicks Ok to accept the DataForm changes. Is there a way to modify that behavior to postpone writing data to CurrentItem to when the user accepts the changes?

EDIT: Here's most of the Xaml for a DataForm I'm using:

xmlns:DataFormControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"

<DataFormControls:DataForm x:Name="dataForm" AutoCommit="False" AutoEdit="False">           
            <DataFormControls:DataForm.EditTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">

                        <DataFormControls:DataField Label="Title">
                            <TextBox Text="{Binding Title, Mode=TwoWay}" Style="{StaticResource TextBoxStyle}"/>
                        </DataFormControls:DataField>

                        <DataFormControls:DataField Label="First Name">
                            <TextBox Text="{Binding FirstName, Mode=TwoWay}"/>
                        </DataFormControls:DataField>

                        <DataFormControls:DataField Label="Middle Name">
                            <TextBox Text="{Binding MiddleName, Mode=TwoWay}"/>
                        </DataFormControls:DataField>

                        <DataFormControls:DataField Label="Last Name">
                            <TextBox Text="{Binding LastName, Mode=TwoWay}"/>
                        </DataFormControls:DataField>
                    </StackPanel>
                </DataTemplate>
            </DataFormControls:DataForm.EditTemplate>
        </DataFormControls:DataForm>

EDIT 2: The workaround I'm using to avoid this behavior is to make a copy of the object to be edited and set it to the DataForm.CurrentItem property, then if the user accepts the edit the data is copied back to the original object. I'm hoping there's a better solution out there.

+1  A: 

Everything is bound to the same instance of the entity. Since the DataForm fields are bound to properties on the entity, when you leave a field, it calls the property setter, changing the value and raising INotifyPropertyChanged.PropertyChanged events. This then informs other bound controls that the value has changed and their bindings are updated.

If you really want to prevent this, you would need to do some hefty work to clone the entity that the DataForm is bound to, and then when the item is committed, update the original with the clone's values. This would not be recommended.

Alternatively, you could have multiple instances of the DomainContext and load the entities twice--once for the DataForm and once for the other displays. After submitting changes on one, you could re-load the entities for the other. This will likely lead to other problems in your application though, so it's also not recommended.

I am curious why the live binding is causing a problem.

Jeff Handley
@jeff i am curious why live binding *wouldn't* cause a problem :-)
Simon_Weaver