Silverlight, Binding Combobox and Textbox to the Same Property
I've got a Silverlight Page with a combo box and text box. The Text property of the text box and the SelectedValue property of the combo box are both bound to the same property on the View Model. The ItemsSource of the combo box provides a limited selection of values: 30, 45, 60, 90.
When a value is selected in the combo box, the text box reflects the new value. When a value is entered in the text box, as long as there is a matching value in the combo box, the combo box changes its' selection to reflect the ne开发者_JAVA百科w value.
When a value is entered in the text box and there is no matching value in the combo box, the binding on the combo box breaks and it displays nothing. Changing the combo box no longer updates the View Model or the text box.
Are there any suggestions for how to make a combo box and text box bind to the same property, even if the values entered into the text box don't always have a matching value in the combo box?
You might need to separate the values in the ViewModel (SelectedItem and Text). Then in the setter of the SelectedItem, update the property the Text property in the ViewModel as well. In the Text property's setter, see if the value exists in the comboBox's itemsSource, and if so, update the SelectedItem in the ViewModel.
Not sure if this will work with your constraints, but that's how I would do it.
精彩评论