开发者

How to bind WPF data: GUI -> Business object -> GUI

I have a binding from WPF textbox to a business object. It is a two way binding wit开发者_如何学Pythonh 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.

private string selectedEmployees;
public string SelectedEmployees
{
  get { return this.selectedEmployees; }
  set
  {
    string sortedValue = // sort value here
    this.selectedEmployees = sortedValue;
    this.OnPropertyChanged("SelectedEmployees");
  }
}


If you have two-way-binding and the BO sorts the values and sets the property after sorting to the new value, this should be reflected by the control.
If not, check that you have implemented INotifyPropertyChanged correct and PropertyChanged fires after the sorting (I assume your BO is not a DependencyObject).

<TextBox.Text>     
       <Binding Path="SelectedEmployees" UpdateSourceTrigger="LostFocus" Mode="TwoWay"/> </TextBox.Text> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜