views:

26

answers:

3

hI,

I am working in wpf - mvvm model.

I have a textbox which holds- "marketName". In xaml I am binding the property - "newmarketname"(which is defined in my viewmodel) to this textbox. Once if user enter a new market name in that text box, the "set" method of the "newmarketname" get called.

In set method of the "newmarketname", I call the PropertyChanged event.

And the property changed handler will call a fucntion, in which I check f the market name is already existing..if "yes"...i will assign string.Empty to the property "newmarketname". So the set method of "newmarketname" again get called followed by PropertyChanged event.

But issue is...even though the value of "newmarketname" is getting changed to empty, it is not getting reflected in UI.

Whats the issue ???

A: 

Are you changing the underlying variable instead of the property? That's what I always do (by mistake).

Grant Crofton
No...am changing property itself...and "on property changed" event is getting called...HEre...the thing is, on property changed event of one property...u r changing it again...that is first from UI...and then from code level
Anish
+1  A: 

WPF will ignore PropertyChanged events that are raised while it is setting the value. One workaround is to use a Converter (even one that just returns the raw value) and set UpdateSourceTrigger to LostFocus. Another is to set IsAsync to True.

This has been fixed in .NET 4.0. See this blog entry for more information: http://karlshifflett.wordpress.com/2009/05/27/wpf-4-0-data-binding-change-great-feature/

Quartermeister
Can u plz tell me how to use this Converter
Anish
A: 

Maybe you could set your binding mode to TwoWay, this way it will reflect both changes from the UI and code behind

<TextBlock Text="{Binding newmarketname, Mode=TwoWay}" />
lifeofmle