Tuesday, May 3, 2011

Expression Blend : Starting a storyboard from ViewModel in silverlight

It is super easy to do. But first a little bit about the setup.
I have a INPC property on my viewmodel like so :

#region IsLoggedIn Property
        private bool _IsLoggedIn = false;
        public bool IsLoggedIn
        {
            get
            {
                return _IsLoggedIn;
            }
            set
            {
                if (_IsLoggedIn == value)
                {
                    return;
                }
                _IsLoggedIn = value;
                RaisePropertyChanged();
            }
        }
        #endregion


The view has a DataContext set to this.
Now based on a change in this boolean value I want to trigger the "LoginStoryboard" that I created in blend.
Easy peasy. Just look for "ControlStoryboard" in the assets panel as shown:
And drag and drop it onto any object in the "Objects and Timeline" panel as shown:
Now select this action in the objects and timeline panel and view its properties in the properties panel. Click New as shown:
Select "DataTrigger" as shown:
Click Ok.
Next databind the Binding property to your property in the ViewModel as shown:
Complete the remaining properties (comparison / Value / Play / LoginStoryboard ) as shown :

All done :)
Enjoy!