WPF DataTemplate/ControlTemplate 和 VS2008 设计器

发布于 2024-08-12 02:22:40 字数 361 浏览 2 评论 0原文

假设您的 WPF 窗口由许多使用 DataTemplates / ControlTemplates 的元素(ItemControls ...)组成 但您想了解每个 DataTemplate 在 VS Designer 中的外观。 更重要的是,如果您将 ControlTemplate 定义为位于另一个文件中的模板,以便能够查看它的内容。

类似于 ASP.NET 中的 MasterPage 和 ChildElements 您始终可以看到您将哪些元素组合在一起。

这在 WPF 中也可能吗? 否则..我对 DataTemplate 所做的每一个更改都可以在耗时的编译和启动后看到。

谢谢各位..

Suppose you have WPF Window composed of many elements that are using DataTemplates / ControlTemplates (ItemControls ... )
But you want to see how every DataTemplate looks like in VS Designer.
What more, if you define a ControlTemplate from as a Template located in another file to be able to view it with the content.

Something like MasterPage and ChildElements in ASP.NET
You can always see what elements have you put together.

Is this possible in WPF too?
Otherwise.. every change I make to the DataTemplate can be seen after a long-time-consuming compilation and start.

Thank you guys..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睡美人的小仙女 2024-08-19 02:22:40

Stefan,

我认为您可以通过创建设计时类来做到这一点,如下面的链接所述:

http://karlshifflett.wordpress.com/2008/10/11/viewing-design- time-data-in-visual-studio-2008-cider-designer-in-wpf-and-silverlight-projects/

希望这会有所帮助。

Stefan,

I think that you can do this by creating Design Time class as described in the link below:

http://karlshifflett.wordpress.com/2008/10/11/viewing-design-time-data-in-visual-studio-2008-cider-designer-in-wpf-and-silverlight-projects/

Hope this helps.

枫以 2024-08-19 02:22:40

要非常简单地完成此操作,只需在构造函数中设置 DataContext:

public MyUserControl()
{
#if !RELEASE
    //DataContext = new CustomerList { Customers = new [] {
    //  new Customer { Name = "Contoso", ZipCode = 12345 },
    //  new Customer { Name = "NorthWind", ZipCode = 12345 },
    //}};
#endif
  InitializeComponent();
  ...
}

请注意代码已被注释掉。当您想查看数据时,只需取消注释代码即可。 #if !RELEASE 可以防止您意外地将示例数据包含在您的版本中(并且避免花费任何 CPU 来加载它)。

如果您的示例数据很大,只需将其放入 XML 或数据库中并加载即可:

public MyUserControl()
{
#if !RELEASE
    //DataContext = XmlSerializerManager.Deserialize<CustomerList>(
    //  File.ReadAllBytes("CustomerSampleData.xml"));
#endif
  InitializeComponent();
  ...
}

无论哪种情况,每当您取消注释代码时,示例数据都会显示在设计器中。执行应用程序时,它将被替换为真实数据。

To do this extremely simply, just set a DataContext in your constructor:

public MyUserControl()
{
#if !RELEASE
    //DataContext = new CustomerList { Customers = new [] {
    //  new Customer { Name = "Contoso", ZipCode = 12345 },
    //  new Customer { Name = "NorthWind", ZipCode = 12345 },
    //}};
#endif
  InitializeComponent();
  ...
}

Notice the fact that the code is commented out. When you want to see data, just uncomment the code. The #if !RELEASE protects you from accidentally including the sample data in your release (and from spending any CPU loading it).

If your sample data is big, just put it in XML or in a database and load it:

public MyUserControl()
{
#if !RELEASE
    //DataContext = XmlSerializerManager.Deserialize<CustomerList>(
    //  File.ReadAllBytes("CustomerSampleData.xml"));
#endif
  InitializeComponent();
  ...
}

In either case, the sample data will show up in the designer whenever you uncomment the code. When executing your application it will be replaced with the real data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文