Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF textbox that only updates binding when enter is pressed

all. I have a usercontrol "NumericTextBox" that only allows numeric entries. I need to exhibit another specialized behaviour, that is, I need it to be able to bind it to a VM value OneWayToSource and only have the VM value update when I press enter while focusing the textbox. I already have the an EnterPressed event that fires when I press the key, I'm just having a hard time figuring out a way to cause that action to update the binding...

like image 400
Kamiikoneko Avatar asked Sep 17 '09 14:09

Kamiikoneko


People also ask

What is Updateourcetrigger WPF?

This is a property on a binding that controls the data flow from a target to a source and used for two-way databinding. The default mode is when the focus changes but there are many other options available, that we will see in this article.

How do you check if Enter key is pressed in TextBox C#?

You can also do this: private void input_KeyDown(object sender, KeyEventArgs e) { if(e. KeyCode== Keys. Enter) { //Your business logic here. } }

How do you make a text box non editable in WPF?

The IsReadOnly property of the TextBox sets the text box read only. By default, it is false. The MaxLength property of the TextBox sets the number of characters allowed to input in a text box.


1 Answers

In your binding expression, set the UpdateSourceTrigger to Explicit.

Text="{Binding ..., UpdateSourceTrigger=Explicit}"

Then, when handling the EnterPressed event, call UpdateSource on the binding expression, this will push the value from the textbox to the actual bound property.

BindingExpression exp = textBox.GetBindingExpression(TextBox.TextProperty);
exp.UpdateSource();
like image 110
decasteljau Avatar answered Oct 18 '22 16:10

decasteljau