使DataTemplate可混合 [英] Making a DataTemplate blendable

查看:121
本文介绍了使DataTemplate可混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为ViewModel可混合(可在表达式混合中设计)制作数据表。当我转到资源并尝试直接编辑DataTemplate,我在Drawingborad上看到的是一个空白矩形。这是因为DataTemplate没有绑定任何东西。当然,我可以创建一个UserControl并在代码中创建一些设计时数据来查看模板,但是现在我必须在资源(编辑)和usercontrol之间来回切换(以查看我的编辑结果)。有没有更直接的方式来编辑和查看我的DataTemplate?

How can I make a Datatemplate for a ViewModel blendable (designable in expression blend). When I go to resources and try to edit the DataTemplate directly all I see on the Drawingborad is a blank rectangle. This is because the DataTemplate is not bound to anything. Of course I can create a UserControl and create some designtime data in code there to see the template but I now would have to switch back and forth between the resource (to edit) and the usercontrol (to see the result of my edit). Isn't there a more direct way to edit and see my DataTemplate?

推荐答案

这是一个很长的一段可以使用,但是Blend有一个名为设计时间数据的功能,可以帮助您。首先很难开始,但一旦你做了很多,这很容易。这样一来,你就成为DataContext的一个很好的模式。

It's a bit of a stretch to use, but Blend has a feature called "Design-Time Data" that can help you out. It's tough to get started at first, but once you do a few it's pretty easy. It kind of forces you into a nice pattern for DataContext as well.

这是一个很好的链接: http://www.robfe.com/2009/08/design-time-data-in- expression-blend-3 /

Here's a good link on the subject: http://www.robfe.com/2009/08/design-time-data-in-expression-blend-3/

以下是几个选项摘录:

设计时间大小


...设计时间属性可以由
安全地被其他工具忽略,他们
在运行时被忽略
(mc:Ignorable指定具有d前缀的
命名空间可以忽略为

...design time properties can be safely ignored by other tools and they are ignored at the runtime (mc:Ignorable specifies that the namespace with the "d" prefix can be ignored).

 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d"

表达式Blend使用两个设计时间
属性(d:DesignWidth,
d:DesignHeight)来指定大小fo r
a控制在设计时使用...

Expression Blend uses two design time properties (d:DesignWidth, d:DesignHeight) to specify a size for a control to be used at design time...

设计时数据源


我偶然遇到d:Datacontext当我
和Blend 3一起玩,尝试
添加一个live数据源到我的
窗口。我以为是要
的行为就像旧的方式
设置一个DataContext,但是当我运行
我的应用程序,没有数据! ...

I stumbled across d:Datacontext when I was playing with Blend 3 and tried adding a "live datasource" to my window. I thought it was going to behave just like the old way of setting a DataContext, but when I ran my application, there was no data! ...

所以结果是,现在我们可以这样写
代码:

So the upshot is, now we can write code like this:

...
<Grid ...
      DataContext="{StaticResource GameDataSource}"
      d:DataContext="{StaticResource DesignTime_DateDataSource}">


请注意,如果您想要第一方支持这些功能。他们是非常好的 - 甚至设计师的设计时间数据,虽然我还没有研究这些功能。

Note that this is for Blend 3 if you want first-party support for these features. They are pretty good - there's even a designer for the design-time data, though I've not looked into those features yet.

应用于DataTemplates

这是我排列的东西,但似乎有效。在这里,我使用设计时数据功能将数据拉入视觉元素的d:DataContext。您必须为需要DataContext集的每个顶级元素执行此操作。

This is something I sorta made up, but it seems to work. Here I'm using the Design-Time data feature to pull data into the visual element's d:DataContext. You'd have to do this for every top-level element that needed a DataContext set.

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <!-- Resource dictionary entries should be defined here. -->
    <DataTemplate x:Key="MyTemplate">
        <TextBlock Text="{Binding Text}" d:DataContext="{StaticResource SampleDataSource}" />
    </DataTemplate>
</ResourceDictionary>

如果您使用DataTemplate与DataType集合,绑定语法有一点更明确,但是它仍然有效:

The binding syntax is a little bit more explicit if you are using a DataTemplate with a DataType set, but it still works:

<DataTemplate DataType="{x:Type vm:MyViewModel}" >
   <TextBlock Text="{Binding Text}" 
              d:DataContext="{Binding Source={StaticResource SampleDataSource}}" />
</DataTemplate>

这个策略可以让您看到DataTemplate在直接编辑时如何工作,但是除非您实际运行该应用程序,否则您将无法在使用该DataTemplate的任何视图中看到结果。这是Blend目前的限制,因为它们似乎没有使用Mocks,而是完全替换对象。如果混合增加了通过点击新数据源 - >基于引用对象 - > MyCustomerObject创建新的假数据源的功能,那么您将在业务中。

This strategy will allow you to see how the DataTemplate will work while editing it directly, however you won't be able to see the result on any view that utilizes that DataTemplate unless you actually run the app. This is a limitation of Blend at the moment due to the fact that they don't appear to be using Mocks, but rather complete replacement objects. If blend ever adds the ability to create a new fake data source by clicking on "New DataSource -> Based on Referenced Object -> MyCustomerObject", then you will be in business.

有可能您可以通过自己的附属物品欺骗来克服这个限制,但最好难度最高。

It's possible you could overcome this limitation with some attached property trickery of your own, but it would be difficult at best.

替代

另一种可以在每个情况下工作的替代方案,但是设置一个用于将真实数据交换出真实数据的StaticResources设置更麻烦一点在运行期间,但在设计师中显示静态样本数据。

An alternative that will work in every situation, but is a bit more cumbersome to setup is setting up StaticResources that swap out fake data for real data during runtime, but in the designer show static sample data.

这是Karl Shifflett的一篇非常棒的文章,其中包含了一些技术和一些视频:
http://karlshifflett.wordpress.com/2008/10/11/viewing-design-time data-in-visual-studio-2008-cider-designer-in-wpf-and-silverlight-projects /

Here's a really great article by Karl Shifflett that includes some of these techniques and a few videos on it: http://karlshifflett.wordpress.com/2008/10/11/viewing-design-time-data-in-visual-studio-2008-cider-designer-in-wpf-and-silverlight-projects/

希望这有帮助,
Anderson

Hope this helps, Anderson

这篇关于使DataTemplate可混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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