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

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

问题描述

我在没有表达式混合的情况下工作,只是在 vs2010 中使用 XAML 编辑器.撇开这一点不谈,我越来越多地看到对设计时数据绑定的需求.对于简单的情况,FallbackValue 属性非常有效(Textboxes 和 TextBlocks 等).但尤其是在处理 ItemsControl 等时,确实需要示例数据在设计器中可见,以便您可以调整和调整控件和数据模板,而无需运行可执行文件.

我知道 ObjectDataProvider 允许绑定到类型,因此可以提供用于可视化的设计时数据,但是有一些杂耍允许绑定真实的运行时数据无需通过加载设计时、虚拟数据和运行时绑定来浪费资源.

我真正想要的是能够让John"、Paul"、George"和Ringo"在 XAML 设计器中显示为我的 ItemsControl,但在应用程序运行时会显示真实数据.

我还知道 Blend 允许一些奇特的属性来定义设计时绑定数据,而这些属性在运行时条件下被 WPF 有效地忽略.

所以我的问题是:

1.如何在 Visual Studio XAML 设计器中利用集合和非平凡数据的设计时绑定,然后顺利切换到运行时绑定?

2.其他人是如何解决这个设计时与运行时数据问题的?就我而言,我不能很容易地为两者使用相同的数据(因为可以使用数据库查询).>

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

解决方案

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

  • 添加命名空间声明

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

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

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

  • 设置设计时数据上下文

效果很好.

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天全站免登陆