开发者

Design-Time Population of ListBox with Sample Data in WPF

I have a view (usercontrol) that contains a listbox. At runtime the listbox will be populated with instances of another view (also a usercontrol). How can I populate the listbox in design-time (in Blend and Cider) with sample views that are the开发者_如何转开发mselves populated with sample data?


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.


You might find the BookLibrary sample application of the WPF Application Framework (WAF) interesting. It uses the new design-time support of Visual Studio 2010 and Expression Blend 4. Please download the .Net4 version of WAF.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜