Fix for design time data in Expression Blend Preview for SL5

Topics: Bugs, UI Architecture
Mar 11, 2012 at 3:42 AM

In the Expression Blend Preview for Silverlight 5, it appears as though they have altered the way they are providing design time data. Unfortunately it broke the design time integration of Caliburn Micro. After some debugging it looks as if they are simply setting the data context to the MarkupExtension that wraps the ViewModel object.The following modification to Bind.DataContextChanged() works around the problem:

 

        static void DataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            if(!Execute.InDesignMode)
                return;

            var enable = d.GetValue(AtDesignTimeProperty);
            if(enable == null || ((bool)enable) == false || e.NewValue == null)
                return;

            var fe = d as FrameworkElement;
            if(fe == null)
                return;

            var viewModel = e.NewValue;

            // Allow for the view model provided by Blend to be the MarkupExtension
            var valueProvider = e.NewValue.GetType().GetMethod("ProvideValue");
            if (valueProvider != null)
            {
                viewModel = valueProvider.Invoke(e.NewValue, new object [] { null } );
            }

            ViewModelBinder.Bind(viewModel, d, string.IsNullOrEmpty(fe.Name) ? fe.GetHashCode().ToString() : fe.Name);
        }

Hope this helps someone.

Cheers,
Phil