<p>I have a databound TextBox in my application like so: (The type of <code>Height</code> is <code>decimal?</code>)</p>
<pre class="lang-xml prettyprint-override"><code>    &lt;TextBox Text=&quot;{Binding Height, UpdateSourceTrigger=PropertyChanged, 
                            ValidatesOnExceptions=True, 
                            Converter={StaticResource NullConverter}}&quot; /&gt;
</code></pre>
<pre class="lang-cs prettyprint-override"><code>    public class NullableConverter : IValueConverter {
        public object Convert(object o, Type type, object parameter, CultureInfo culture) {
            return o;
        }

        public object ConvertBack(object o, Type type, object parameter, CultureInfo culture) {
            if (o as string == null || (o as string).Trim() == string.Empty)
                return null;
            return o;
        }
    }
</code></pre>
<p>Configured this way, any non-empty strings which cannot be converted to decimal result in a validation error which will immediately highlight the textbox. However, the TextBox can still lose focus and remain in an invalid state. What I would like to do is either:</p>
<ol>
<li>Not allow the TextBox to lose focus until it contains a valid value.</li>
<li>Revert the value in the TextBox to the last valid value.</li>
</ol>
<p>What is the best way to do this?</p>
<p><strong>Update:</strong></p>
<p>I've found a way to do #2. I don't love it, but it works:</p>
<pre><code>private void TextBox_LostKeyboardFocus(object sender, RoutedEventArgs e) {
    var box = sender as TextBox;
    var binding = box.GetBindingExpression(TextBox.TextProperty);
    if (binding.HasError)
        binding.UpdateTarget();
}
</code></pre>
<p>Does anyone know how to do this better? (Or do #1.)</p>

[{'answer_id': 44362, 'author': 'Dillie-O', 'author_id': 71, 'author_profile': 'https://Stackoverflow.com/users/71', 'pm_score': 0, 'selected': False, 'text': "<p>It sounds to me that you'll want to handle two events:</p>\n\n<p>GotFocus: Will trigger when the textbox gains focus. You can store the initial value of the box.</p>\n\n<p>LostFocus: Will trigger when the textbox loses focus. At this point you can do your validation and decide if you want to roll back or not.</p>\n"}, {'answer_id': 47663, 'author': 'David Schmitt', 'author_id': 4918, 'author_profile': 'https://Stackoverflow.com/users/4918', 'pm_score': 3, 'selected': True, 'text': '<p>You can force the keyboard focus to stay on the <code>TextBox</code> by handling the <code>PreviewLostKeyBoardFocus</code> event like this:</p>\n<pre class="lang-xml prettyprint-override"><code>     &lt;TextBox PreviewLostKeyboardFocus=&quot;TextBox_PreviewLostKeyboardFocus&quot; /&gt; \n</code></pre>\n<pre class="lang-cs prettyprint-override"><code>     private void TextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) {\n         e.Handled = true;\n     }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/44298', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/317/']