How do you bind the VisualStateManager state of a control to a property in you viewmodel?
Can it be done?
How do you bind the VisualStateManager state of a control to a property in
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Actually you can.
The trick is to make an Attached property and add a property changed callback that actually calls
GoToState:You can then set this property in you xaml and add a binding to your viewmodel like any other:
NameandStateare regular properties in the viewmodel. WhenNameis set in the viewmodel, either by the binding or something else, it can change theStatewitch will update the visual state.Statecould also be set by any other factor and still it would update the view state on the textbox.Since we’re using a normal binding to bind to Status, we can apply converters or anything else that we’d normally be able to do, so the viewmodel doesn’t have to be aware that its actually setting a visual state name, State could be a bool or an enum or whatever.
You can also use this approach using the wpftoolkit on .net 3.5, but you have to cast
targetto aControlinstead of aFrameworkElement.Another quick note on visual states, make sure you don’t name your visual states so that they conflict with the built in ones unless you know what you’re doing. This is especially true for validation since the validation engine will try and set its states everytime the binding is updated (and at some other times as well). Go here for a reference on visual state names for diffrent controls.