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

本文是小编为大家收集整理的关于使得DataTemplate可以混合的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何为ViewModel Blendable(在表达式混合物中设计)制作DataTemplate.当我转到资源并尝试直接编辑DataTemplate时,我在Drawsborad上看到的所有内容都是空白的矩形.这是因为DatateMplate不绑定到任何内容.当然,我可以创建一个USERCONTROL,并在那里的代码中创建一些Designtime数据以查看模板,但是现在我必须在资源(编辑)和USERCONTROL之间来回切换(以查看我的编辑结果).没有一种更直接的方法来编辑和查看我的datatemplate吗?

推荐答案

这有点伸展,但是Blend具有称为"设计时数据"的功能,可以帮助您.起初很难开始,但是一旦您做了一些事情,这很容易.这也迫使您成为DataContext的一个不错的模式.

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

这是一些选择摘录:

在设计时大小

...设计时间属性可以是 其他工具安全地忽略了 在运行时被忽略 (MC:无知指定 具有" D"前缀的名称空间可以是 忽略).

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

表达混合使用两个设计时间 属性(d:designWidth, D:DesignHeight)指定尺寸 在设计时使用的控件...

在设计时数据源

我偶然发现了d:datacontext 在玩Blend 3并尝试了 向我的 窗户.我以为会去 就像旧方式一样 设置DataContext,但是当我运行时 我的应用程序,没有数据! ...

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

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

请注意,如果您想对这些功能进行第一方支持,则这是对Blend 3的.它们非常好 - 尽管我还没有研究这些功能,但即使是设计时间数据的设计师.

应用于DataTemplates

这是我构成的,但似乎有效.在这里,我正在使用设计时间数据功能将数据拉入视觉元素的d:datacontext.您必须为每个需要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,则绑定语法更为明确,但仍然有效:

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

此策略将允许您在直接编辑的同时查看DataTemplate如何工作,但是您将无法在任何使用该DataTemplate的视图上看到结果,除非您实际运行应用程序.这是当前混合的限制,因为它们似乎没有使用模拟,而是完整的替换对象.如果Blend曾经通过单击"基于引用的对象 - > mycustomerobject"来创建新的假数据源的功能,那么您将开始业务.

您可能可以通过自己的一些附带属性骗局来克服这一限制,但最充其量很难.

替代

一种将在每种情况下都可以使用的替代方案,但是设置更麻烦的是设置静电率,可以在运行时将伪造数据交换为真实数据,但在设计师中显示静态样本数据.

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

希望这会有所帮助, 安德森

其他推荐答案

此策略将使您看到 DatateMplate如何工作时 直接编辑它,但是您不会 能够在任何视图上看到结果 除非 您实际运行该应用程序.这是一个 目前的混合限制 事实是他们似乎没有 使用模拟,而是完整 替换对象.如果融合在一起 添加了创建新假货的能力 通过单击"新的"来源 数据源 - >基于引用 对象 - > mycustomerObject",然后您 将开展业务.

如果我想使用Acutal ViewModel模型,我想这是创建实际ViewModel实例的最佳方法,并使用D:DataContext(例如使用objectdataprovider或x:static)

>

本文地址:https://www.itbaoku.cn/post/627385.html

问题描述

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?

推荐答案

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.

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

Here's a few choice excerpts:

On Design-Time Sizes

...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"

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

On Design-Time Data Sources

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.

Applying To DataTemplates

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>

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>

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.

Alternative

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.

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/

Hope this helps, Anderson

其他推荐答案

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.

If I want to use acutal ViewModel mocks I guess it is the best way to create actual ViewModel instances and the references them with d:DataContext (e.g. using a ObjectDataProvider or x:Static)