WPF how to control the ListCollectionView binding method?

1.3k views Asked by At

i'm having a simple ObservableCollection list which i bind to a form using ListCollectionView.

enter image description here

the problem i would like to control the binding method. means that only when i press the OK button then the property is changed.

as now, if i change the "first name" and navigate, then the value is saved.

for example, if i change the "first name" to XXX and navigate next and then back, i would see XXX instead of x...

this is the MainWindow.xaml:

public partial class MainWindow : Window
{
    private ListCollectionView view;

    private PersonRepository _personRepository;


    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        _personRepository = new PersonRepository();

        this.DataContext = _personRepository.Persons;

        view = (ListCollectionView)CollectionViewSource.GetDefaultView(this.DataContext);
    }


    private void BackButton_Click(object sender, RoutedEventArgs e)
    {
        view.MoveCurrentToPrevious();

        view.Refresh();
    }

    private void OkButton_Click(object sender, RoutedEventArgs e)
    {

    }

    private void NextButton_Click(object sender, RoutedEventArgs e)
    {
        view.MoveCurrentToNext();
    }
}

and Person class is:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Age { get; set; }
    public string Description { get; set; }
}

the Repository is ObservableCollection with dummy data... my xaml fields bind as: Text="{Binding FirstName, UpdateSourceTrigger=Explicit}"

2

There are 2 answers

1
Kent Boogaart On

You could have Person implement IEditableObject. Then, you would call EndEdit from the OK button, but CancelEdit from other navigation buttons.

That said, I would find this interface unintuitive.

0
hbarck On

I'd do this by using a BindingGroup. I'd choose a container, e.g. a GroupBox, or simply the Grid that lays out the controls which edit the Person class, make sure that the container contains all these controls and that the current Person object is its DataContext, and specify a BindingGroup in it's XAML definition.

This would have the effect that the default UpdateSourceTrigger on all Bindings to the current Person changes from LostFocus to Explicit. This means that the controls which you use to edit the Person object won't update their bound propertoes automatically, but will wait until you call CommitEdit on the BindingGroup, which is obviously what you will do when the OK-Button is pressed. As Kent already suggested, you'd call CancelEdit in the navigation buttons, in order to keep the current Person from being updated if you don't press OK. The logic would be the same as with IEditableObject, only that you don't have to change your Person class, but instead use the BindingGroup's behaviour to achieve what you want.