How to stop WPF binding from ignoring the PropertyChanged event that it raised?

I have a TextBox associated with the ViewModel Text property with the following setting:

Xaml

<TextBox Text="{Binding Text}"/> 

FROM#

 public class ViewModel : INotifyPropertyChanged { public string Text { get { return m_Text; } set { if (String.Equals(m_Text, value)) { return; } m_Text = value.ToLower(); RaisePropertyChanged("Text"); } } // Snip } 

When I inject some material into a TextBox, it successfully sets the Text property to the ViewModel. The problem is that WPF ignores the event with the changed property that is generated by its own update. This leads to the fact that the user does not see the entered text converted to lowercase.

How can I change this behavior so that the TextBox is updated with lowercase text?

Note. This is just an example that I used to illustrate the issue of ignoring WPF events. I am not interested in converting strings to lowercase or any problems with String.Equals (string, string).

+3
data-binding wpf
source share
4 answers

This can be done by raising the event in a separate dispatch call using Dispatcher.BeginInvoke

Define a delegate:

 private delegate void RaisePropertyChangedDelegate(string property); 

Then use the following to raise an event

 Dispatcher.CurrentDispatcher.BeginInvoke( DispatcherPriority.Normal, new RaisePropertyChangedDelegate(RaisePropertyChanged), "Text"); 
+2
source share

This seems to be fixed in the next version of WPF: Karl Schifflett's blog

+2
source share

I remember Rocky Lotka complaining about this behavior in an old .NET Rocks episode. (Looking for half an hour ...) ah, here we are. Episode 169 dated March 2006:

So, if you have a detailed form in Windows Forms, then you bind all your properties to those text fields and the user prints things into a text field and tabs, of course, this value is your object, but your object can change the value, maybe there is some kind of manipulation that says all letters should be in uppercase. These are business rules to make it an object. If you do, in the user interface. In other words, the user can enter types a, b, c, lowercase, open the tab. In lowercase a, b, c will remain in the text box. Then later, when they change another field, then they will mean the object is uppercase value, right? So the object is uppercase a, b, c the user interface does not display lowercase letters correctly, the user then changes some other controls and tabs from this control, all of a sudden a, b, c in uppercase is displayed in the text box that they werent on.

Rocky does not really offer a solution to the problem, and I would take a chance if he did not deal with this, maybe there is no good answer. Perhaps you need to subscribe to the PropertyChanged event on your object from the code and manually update the binding when the corresponding property has changed.

ps. This does not directly answer your question, but in the example you provided, you can set CharacterCasing to a TextBox so that it accepts only lowercase characters.

0
source share

You need to force update the binding target (see this post on MSDN ). However, this was changed / broken in WPF4, so you need to force update the source code (see this post ).

0
source share

All Articles