tags:

views:

532

answers:

3

I am trying to bind to a combobox text with the IsEditable property set to true. I have a property in my viewmodel which is bound to the text.

I want to validate on the text being typed in the text of the combobox, and restrict some values that the user is typing in. So some will be allowed, and some not, and these need to set the combobox back to its old value.

I do this in the view model and I have tried setting my text property in my view model explicity to the old value or just ignoring the change and raising that the property has been changed, but for the life of me it will not refresh the text back to the old value.

Is this because the combobox is editable, and it has the text caret and focus somewhere in the text of the combobox.

Basically, I want it to refresh back to the previous text when I restrict some typing in the combobox during in editing. Anyone have any ideas to reset the text back to its old value through the ViewModel. Thanks in advance!

A: 

Is the viewmodel property you are binding to created as a DependencyProperty? This is probably the problem you are facing http://stackoverflow.com/questions/320028/two-way-binding-in-wpf

If you don't want to create a Dependency property then you need to implement INotifyProperty changed and manually force the update in the Property changed event.

Simon Fox
I have used a dependency property and INotifyProperty changed. Both did not work. The binding on the Text property of the combobox is TwoWay.
prem_data
where abouts are you performing the validation of the users input?
Simon Fox
I have done it on the onpropertychanged callback from the dep propAnd I have done it in the setter of the text property in the view model, calling another method to validate the text.
prem_data
What you could do is create a Convertor class for the binding (implement IValueConvertor), in the Convert method check the value provided and if you don't like it return Binding.DoNothing();
Simon Fox
"could try" might be better wording :)
Simon Fox
could be worth a try, maybe!
prem_data
A: 

I think this is because of a 'bug' in WPF not refreshing the UI if you change the value of a property in the setter. You can workaround it by implementing an IdentityConverter that force the UI to refresh as per this arcticle.

Julien Poulin
I tried what was suggested here, but to my chagrin it only works when the control loses focus as stated in the article. Which is not the behavior I require.
prem_data
+1  A: 

Thanks for your replies. But I could never get it to work instead, I made my own UserControl which comprises a textbox overlayed over a combobox, and manipulate those two controls to meet my needs. A long way to go to solve a simple problem, but it works in the end.

prem_data