什么方法可用于WPF中的虚拟设计时数据? [英] What approaches are available to dummy design-time data in WPF?

查看:139
本文介绍了什么方法可用于WPF中的虚拟设计时数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在没有表达式的情况下工作,只是在vs2010中使用XAML编辑器。除了这种智慧之外,我越来越看到需要设计时数据绑定。对于简单的情况, FallbackValue 属性非常好地工作(Textboxes和TextBlocks等)。但是特别是当处理 ItemsControl 等时,真正需要样本数据在设计器中可见,以便您可以调整和调整控件和数据模板,而无需运行可执行。



我知道 ObjectDataProvider 允许绑定到一个类型,从而可以提供设计时间数据,但是随后,通过加载设计时间,虚拟数据和运行时绑定,无法浪费资源,从而允许真实的运行时数据进行绑定。



我想要的是能够在XAML设计师中展示John,Paul,George和Ringo作为我的 ItemsControl 中的样式项目,但是在应用程序运行时会显示真实数据。



我也知道Blend允许一些花哨的属性来定义WPF在运行时条件下有效忽略的设计时间绑定数据。



所以我的问题是:



1。如何在视觉工作室XAML设计器中利用集合和非平凡数据的设计时绑定,然后顺利地交换到运行时绑定?



2。其他人如何解决这个设计时间与运行时数据问题?在我的情况下,我不能轻易地使用相同的数据(就像数据库查询一样)。 p>

3。他们的表达方式是否可以混合使用,我可以用于数据集成的XAML设计?(我知道有一些替代方案,但我特别想要一些可以使用的东西,看到绑定的样本数据等)

解决方案

使用VS2010可以使用设计时属性(适用于SL和WPF)。我通常有一个模拟数据源,所以这只是一个问题:




  • 添加命名空间声明

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


  • 将模拟数据上下文添加到窗口/控制资源

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


  • 设置设计时数据环境

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




工作得很好。


I am working without expression blend and just using the XAML editor in vs2010. The wisdom of this aside, I am increasingly seeing a need for design-time data binding. For simple cases, the FallbackValue property works very nicely (Textboxes and TextBlocks, etc). But especially when dealing with ItemsControl and the like, one really needs sample data to be visible in the designer so that you can adjust and tweak controls and data templates without having to run the executable.

I know that ObjectDataProvider allows for binding to a type, and thus can provide design-time data for visualizing, but then there is some juggling to allow for the real, run-time data to bind without wasting resources by loading loading both the design time, dummied data and the runtime bindings.

Really what I am wanting is the ability to have, say, "John", "Paul", "George", and "Ringo" show up in the XAML designer as stylable items in my ItemsControl, but have real data show up when the application runs.

I also know that Blend allows for some fancy attributes that define design time binding data that are effectively ignored by WPF in run-time conditions.

So my questions are:

1. How might I leverage design-time bindings of collections and non-trivial data in the visual studio XAML designer and then swap to runtime bindings smoothly?

2. How have others solved this design-time vs. runtime data problem? In my case, i cannot very easily use the same data for both (as one would be able to with, say, a database query).

3. Are their alternatives to expression blend that i could use for data-integrated XAML design? (I know there are some alternatives, but I specifically want something I can use and see bound sample data, etc?)

解决方案

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.

这篇关于什么方法可用于WPF中的虚拟设计时数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆