Can I preview my DataTemplate(s) in my collection-based controls in the designer?

心已入冬 提交于 2019-12-24 12:18:32

问题


I have an application that I've near-finished that makes use of multiple Data Templates. Specifically, I have a ListView and a DataGrid.

I've created some special formatting for these items and I'd like to preview one of these list items, so that I can tweak the visual layout, coloring, padding, margins, etc.

Can this be done within the Visual Studio designer, or am I forced to design these templates by hand in Xaml, and preview by running my application?


回答1:


you can use a design time viewmodel.

Add these attributes to the usercontrol / window:

xmlns:designTime="clr-namespace:NamespaceToYourDesignTimeViewmodel"
         mc:Ignorable="d"
         d:DataContext="{d:DesignInstance Type=designTime:DesignTimeMyViewModel, IsDesignTimeCreatable=True}"

Assuming your design time viewmodel has the same structure as the run time viewmodel (you may choose to enforce that with an interface); then you can create design time data in your design time view model and see it in the designer.

For example your design time view model may look like:

public class DesignTimeMyViewModel : IMyViewModel
{
    public string Name { get; set; }

    public DesignTimeMyViewModel()
    {
        this.Name = "Design time data";
    }
}

The interface will enforce the consistency of structure between runtime and designtime; and the design time data can just be hardcoded to whatever you need to see in the VS XAML designer.

This is a simple example, but the same principle can be extended to collections of complex objects. The designer will still bind the design time data in the same way as it does at runtime.



来源:https://stackoverflow.com/questions/28987860/can-i-preview-my-datatemplates-in-my-collection-based-controls-in-the-designer

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