Equivalent to EndEdit in WPF

I have a WPF window containing a TextBox. I executed a command that runs on Crtl-S, which saves the contents of the window. My problem is that if the text field is an active control and I recently edited the text in the text field, the last changes to the text field are not made. I need to exit the text box to get the changes.

In WinForms, I usually call EndEdit on the form, and all pending changes will be committed. Another alternative is to use the onPropertyChange binding rather than onValidation, but I would prefer not to.

What is the WPF equivalent for EndEdit or what template is used in this type of script?

Thanks,

+6
c # data-binding wpf textbox
source share
6 answers

To avoid having to go to the tab, you can simply change the UpdateSourceTrigger property of the control bindings. Try the following:

<TextBox.Text> <Binding Path="MyProperty" UpdateSourceTrigger="PropertyChanged"/> </TextBox.Text> 

This tells WPF to update the support object when the Text property changes. This way, you don’t have to worry about leaving. Hope this helps!

EDIT:

The accepted answer for the next SO question provides the ability to automatically run validation rules for the page. You can change it to call UpdateSource () for all BindingExpression objects.

Link

+4
source share

Based on Pwninstein's answer, I now applied EndEdit in my common class for WPF Views / Windows, which will look for bindings and force them to be updated, the code below;

Code below;

 private void EndEdit(DependencyObject parent) { LocalValueEnumerator localValues = parent.GetLocalValueEnumerator(); while (localValues.MoveNext()) { LocalValueEntry entry = localValues.Current; if (BindingOperations.IsDataBound(parent, entry.Property)) { BindingExpression binding = BindingOperations.GetBindingExpression(parent, entry.Property); if (binding != null) { binding.UpdateSource(); } } } for(int i=0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { DependencyObject child = VisualTreeHelper.GetChild(parent, i); this.EndEdit(child); } } protected void EndEdit() { this.EndEdit(this); } 

In my Save command, now I just call the EndEdit method, and I don’t have to worry about choosing the binding method of other programmers.

+6
source share

You can force specific bindings using the following code:

 var bindingExpression = txtInput.GetBindingExpression(TextBox.TextProperty); bindingExpression.UpdateSource(); 

Doing this is generally difficult, because there is no general way to get all the bindings, and you do not want all of them to be updated.

+5
source share

I do not agree with ArielBH. The problem is the interaction between the keyboard and logical focus, and if you have not changed all the triggers for updating the data binding to PropertyChanged, you can skip some updates to the source data in certain scenarios (for example, clicking the buttons on the toolbar). For example, the default update trigger for TextBox.Text is LostFocus, and clicking on the toolbar button does not blur the active focus of the TextBox.

If you have a control registration mechanism, you can force the data to be bound to the source update in the same place where you will call EndEdit in the WinForms application. It is not neat or elegant, but it does its job.

If someone came up with a better solution, I will be ears too.

+3
source share

HI, Well, when using WPF you need to adapt to a different way of thinking.

I would bind the TextBox Text property to one of my properties (Model, ViewModel, Code-Behind, no matter what pleases you). Therefore, when you process CTRL + S, you simply go to the clr property, which is bound, and continue happily with all the data you need.

Hope that helps you if you need code examples, leave me a comment. Ariel

+1
source share

I believe that you should declare a binding group and then refer to that binding group in code. I put mine in the root element of the Window so that it binds all controls to the window.

 <Window.BindingGroup> <BindingGroup /> </Window.BindingGroup> this.BindingGroup.CommitEdit(); 
+1
source share

All Articles