How to selectively update a model in wpf

后端 未结 1 1454
情书的邮戳
情书的邮戳 2021-01-24 20:23

I have a list of models displayed in a listview when the listviewitem is clicked I open a dialog that is bound to the listviewitem\'s data model and allows the user to edit the

相关标签:
1条回答
  • 2021-01-24 20:40

    You can change all your bindings to be "explicit", in that they don't automatically push the value back to the source, and then force them to update when the OK button is clicked.

    For example, let's bind a TextBox to the model's "Foo" property with an explicit update mode:

    <TextBox x:Name="fooEdit" Text="{Binding Foo,UpdateSourceTrigger=Explicit}" />
    

    Then when the OK button is clicked:

    BindingExpression be = fooEdit.GetBindingExpression(TextBox.TextProperty);
    be.UpdateSource();
    

    This can be a little bit cumbersome if you have lots of controls, but it gives you total control over when the underlying properties are updated from their bound controls.

    Update

    I should add that you should also look at the IEditableObject interface, which is designed for the scenario you describe. If you can implement that on your model, or an intermediate ViewModel, that makes life much easier.

    0 讨论(0)
提交回复
热议问题