I have a binding from WPF textbox to a business object. It is a two way binding with update on LostFocus. If I write in the textbox it updates the BO. So far so good.
<TextBox.Text>
<Binding Path="SelectedEmployees"
UpdateSourceTrigger="LostFocus" />
</TextBox.Text>
But I would like to get it to do the following: If I enter “1, 10, 8, 9” then the BO gets updated and the BO sorts the input to “1, 8, 9, 10”. Then the textbox is updated with this processed data from the BO.
How do I get the binding to update the textbox again? Do I have to do this “manually” through code behind?
The TextBox’s UpdateSourceTrigger property has a default value of LostFocus, so you don’t need to set it explicitly. Assuming that your SelectedEmployees property has a getter and a setter, then the binding will work two way by default.
Therefore, all you should need to do is make sure you’re implementing INotifyPropertyChanged on your business object (or as a wrapper property on your view model), and in your SelectedEmployees setter, do your ordering of the input, and set the backing field to this ordered value, and invoke the PropertyChanged event to invalidate the binding and have the UI pick up the updated (sorted) value via the SelectedEmployees getter.