Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF TextBox displayed value is not equal to TextBox.Text
    text
    copied!<p>I check the postal code for about 60 countries. The user enters a postal code which is checked against the rules for that country. The US is an exception where I can auto-fill the value based on other street address information already entered. In some ways it's basically an auto-correction feature. The problem is the auto-filled value never re-displays if it matches the previous .Text value - the 'erroneous' zip code just entered by the user remains displayed!</p> <p>For example, field contains "12345", user deletes the "5", my property setter puts the value back to "12345", the binding getter returns "12345" (and at this point my _TextChanged event fires) but WPF still displays the user edited text "1234"!</p> <p>How do I force WPF to redisplay the value?</p> <p>To simplify the example I have hard-coded the ZipCode value (in the app it is computed in a separate method):</p> <pre><code>public String ZipCode { get { return _address.ZipCode; } set { _address.ZipCode = "12345"; RaisePropertyChanged("ZipCode"); } } </code></pre> <p>Here is the XAML:</p> <pre><code>&lt;TextBox Name="ZipBox" Text="{Binding Path=ZipCode, UpdateSourceTrigger=PropertyChanged}" TextChanged="ZipBox_TextChanged" /&gt; </code></pre> <p>WPF knows the field changed cause it always fires this event. But none of those methods force WPF to correctly display "12345".</p> <pre><code>private void ZipBox_TextChanged(object sender, TextChangedEventArgs e) { ZipBox.InvalidateVisual(); ZipBox.InvalidateMeasure(); ZipBox.UpdateLayout(); } </code></pre> <p>Edit: I also have created a custom static extension method .Refresh(this UIElement uiElement) which calls UIElement.Dispatcher.Invoke() and that also does not force the redraw.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload