providing designtime ViewModel data for Blend and VS

半城伤御伤魂 提交于 2019-12-22 09:57:19

问题


In a MVVM based application, what options do I have to provide ViewModel data at design time, so our designers can actually see something in Blend3 (and VS 2008). How are you doing this? Can I utilize mc:ignorable for this somehow?


回答1:


Yes, Expression Blend can help you with this. Use the "Data" tab to create sample data that has the same shape as your production data. When you create the data source, be sure to uncheck "Enable sample data when application is running".

Sample Data http://www.smips.com/brad/stackoverflow/design-model1.jpg

After you've created your sample data, set the DataContext of your page to the sample data in the XAML. This will let the designers see the sample data when they open the page in Blend.

<navigation:Page DataContext={StaticResource MyFakeDesignData}

In the code for the Loaded handler, write code to set the DataContext to the real model. Since this code will only run when the app is running, and you've told your sample data to not be available when the app is running, this means the running app will get the real data.

private void Home_Loaded(object sender, RoutedEventArgs e)
{
    DataContext = new MyRealViewModel();
}



回答2:


Using VS2010 you can use Design-Time attributes (works for both SL and WPF). I usually have a mock data-source anyway so it's just a matter of:

  • Adding the namespace declaration

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

  • Adding the mock data context to window/control resources

    <UserControl.Resources> <ViewModels:MockXViewModel x:Key="DesignViewModel"/> </UserControl.Resources>

  • Setting design-time data context

    <Grid d:DataContext="{Binding Source={StaticResource DesignViewModel}}"> ...

Works well enough.



来源:https://stackoverflow.com/questions/1335667/providing-designtime-viewmodel-data-for-blend-and-vs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!