Can assign value to a property on property changed event?
I am working in wpf - mvvm model.
开发者_StackOverflowI 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 if 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 the issue is: even though the value of "newmarketname" is getting changed to empty, it is not getting reflected in the UI.
What's the issue?
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/
Are you changing the underlying variable instead of the property? That's what I always do (by mistake).
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}" />
精彩评论