---
title: "WPF: Need a TextBox to have some of the advanced features of a DataGrid cell"  
description: "WPF: Need a TextBox to have some of the advanced features of a DataGrid cell"  
author: "Anonymous User"  
published: 2013-12-18  
updated: 2013-12-18  
canonical: https://www.mindstick.com/forum/1803/wpf-need-a-textbox-to-have-some-of-the-advanced-features-of-a-datagrid-cell  
category: "wpf"  
tags: ["wpf"]  
reading_time: 2 minutes  

---

# WPF: Need a TextBox to have some of the advanced features of a DataGrid cell

My [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) implements a simple [Calculation](https://www.mindstick.com/forum/160723/factorial-calculation) Chain, as spreadsheets do. It has a TextBox into which the user types some numerical values and as soon as it is edited, the code proceeds to update all the dependent fields.

My scarce [experience](https://www.mindstick.com/articles/12276/how-timely-order-deliveries-can-improve-customer-experience) with TextBox tells me that an event can be triggered each time the [user clicks](https://answers.mindstick.com/qa/34556/if-a-user-clicks-all-the-google-ads-on-my-site-is-it-foul) a character. This [behavior](https://www.mindstick.com/forum/159354/runtimeexception-and-exception-behavior) is not good: Suppose the person types the value "1234": the calculation chain would be performed for "1", next for "12", then for "123" and finally for "1234".

I need a way to know when the [editing](https://www.mindstick.com/startup/27/photoroom-the-ai-powered-app-transforming-photo-and-video-editing) ended. Maybe the rectangle could become blue upon Enter, like those in DataGrid. The user should be able to leave with the Tab key or by clicking elsewhere.

Also: an empty TextBox is not the same thing as containing a zero! An empty TextBox's [content](https://www.mindstick.com/articles/13019/get-the-best-content-marketing-pricing-packages-for-your-brand) should be [recognized as](https://www.mindstick.com/forum/34352/the-term-update-database-is-not-recognized-as-the-name-of-a-cmdlet-function) undefined.

The [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time) then is: What kind of control and ancillary [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) do I need?

## Replies

### Reply by Anonymous User

Hi John,\

It sounds like you only want to perform your calculation when the TextBox has LostFocus

In your view you will have

```
<Window...>    <TextBox LostFocus="Perform_Calculation"/></Window>
```

Then in your code-behind define that method

```
private void Perform_Calculation(object sender, RoutedEventArgs e){    // calculation logic here...    // probably also where you will check if the Text is empty (nothing to be done)}
```

Alternatively, you can use a TwoWay binding and the default PropertyChangeTrigger on a TextBox should be LostFocus - so you should only get your property update when the user has moved away from the TextBox.

Note that this won't satisfy your hope of also handling it on the press of an Enter, as the TextBox would still have focus. To enable this also, extend it slightly

```
<Window...>    <TextBox LostFocus="Perform_Calculation" KeyDown="TextBox_KeyDown"/></Window>private void TextBox_KeyDown(object sender, KeyEventArgs e){    if (e.Key == Key.Return)    {        // perform calculation here (probably method that you will also call        // from LostFocus handler to avoid duplication)    }}
```


---

Original Source: https://www.mindstick.com/forum/1803/wpf-need-a-textbox-to-have-some-of-the-advanced-features-of-a-datagrid-cell

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
