Expression Blend 示例数据和数据模板
我正在使用 Expression Blend 的“来自类的示例数据”功能为 MVVM WPF 项目中的 ViewModel 生成示例数据。虽然这在大多数情况下工作正常,但当我使用 ContentPresenter 来呈现视图模型时,它似乎让我失败,并且它应该使用的 DataTemplate 没有 Key,而只有 DataType。 它不使用模板呈现示例数据,而是仅显示带有前缀的类名称。
举个例子,拿这个 ViewModel
public class TestClass
{
public string TestString { get; set; }
}
这个 DataTemplate
<DataTemplate DataType="{x:Type my:TestClass}" >
<TextBlock Text="{Binding TestString}" />
</DataTemplate>
和这个 XAML
<ContentPresenter Content="{Binding MyPropertyContainingATestClass}" />
现在,在运行时,一切都应该是这样,但是在设计期间 Blend 显示“_di0.MyNameSpace.TestClass”而不是 TestString 的内容。 我认为,这是因为示例数据函数生成的类虽然具有相同的属性等,但实际上并不是所需的类型。有办法解决这个问题吗?我更愿意使用这种方式来集成示例数据,而不是在我的实际视图模型中包含所有这些无意义的数据。
I am using the "Sample data from class" function of Expression Blend to generate sample data for my ViewModels in my MVVM WPF project. While this works fine most of the time, it seems to fail me when I am using a ContentPresenter to present the viewmodel, and the DataTemplate it should use has no Key, but only a DataType.
Instead of rendering the sample data using the template, it just displays the classes name with a prefix.
As an example, take this ViewModel
public class TestClass
{
public string TestString { get; set; }
}
this DataTemplate
<DataTemplate DataType="{x:Type my:TestClass}" >
<TextBlock Text="{Binding TestString}" />
</DataTemplate>
and this XAML
<ContentPresenter Content="{Binding MyPropertyContainingATestClass}" />
Now, during runtime, everything is at it should be, but during design Blend shows "_di0.MyNameSpace.TestClass" instead of the content of TestString.
I assume, this is because the classes generated by the sample data function, while having the same properties etc are not really of the needed type. Is there a way around that? I'd prefer to use this way to integrate sample data, and not have all this meaningless data in my actual viewmodels.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于您有一个 DataTemplate 而不是 ContentTemplate。尝试将 DataTemplate 包装在 ContentTemplate 中,如本文所示:
http://msdn.microsoft.com/en -us/library/system.windows.controls.contentcontrol.contenttemplate.aspx
我很抱歉没有时间测试它。
I think the problem is that you have a DataTemplate and not a ContentTemplate. Try wrapping the DataTemplate in a ContentTemplate like this article shows:
http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.contenttemplate.aspx
I apologize for not having time to test it.
一种可能的解决方法是使用 DataTemplateSelector 根据类名称而不是类类型来选择模板。
更改 DataTemplate 以
创建 DataTemplateSelector
并像使用它一样
工作。
我对这种方法并不是很满意,因为它是很多不必要的工作并且依赖于魔术字符串,但总比没有好。
A possible workaround is using a DataTemplateSelector that chooses the template based on the classes names instead of their types.
Changing the DataTemplate to
creating a DataTemplateSelector
and using it like
works.
I am not really satisfied with that approach, since it is a lot of unnecessary work and relies on magic strings, but its better than nothing.