Expression Blend 示例数据和数据模板

发布于 2024-11-03 13:11:01 字数 811 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素年丶 2024-11-10 13:11:01

我认为问题在于您有一个 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.

苏璃陌 2024-11-10 13:11:01

一种可能的解决方法是使用 DataTemplateSelector 根据类名称而不是类类型来选择模板。

更改 DataTemplate 以

<DataTemplate x:Key="TestClassTemplate" DataType="{x:Type my:TestClass}" >
    <TextBlock Text="{Binding TestString}" />
</DataTemplate>

创建 DataTemplateSelector

public class TestTemplateSelector : DataTemplateSelector
{

    public override DataTemplate SelectTemplate(
                                       object item, 
                                       DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;

        if (element == null || item == null)
            return null;

        if (item.GetType().Name.Contains("TestClass"))
            return element.FindResource("TestClassTemplate") as DataTemplate;

        // Check for other classes here...

        return null;
    }
}

并像使用它一样

<!-- in ressources -->
<local:TestTemplateSelector x:Key="TestTemplateSelector" />

<ContentPresenter Content="{Binding MyPropertyContainingATestClass}"
                  ContentTemplateSelector="{StaticResource TestTemplateSelector}" />

工作。

我对这种方法并不是很满意,因为它是很多不必要的工作并且依赖于魔术字符串,但总比没有好。

A possible workaround is using a DataTemplateSelector that chooses the template based on the classes names instead of their types.

Changing the DataTemplate to

<DataTemplate x:Key="TestClassTemplate" DataType="{x:Type my:TestClass}" >
    <TextBlock Text="{Binding TestString}" />
</DataTemplate>

creating a DataTemplateSelector

public class TestTemplateSelector : DataTemplateSelector
{

    public override DataTemplate SelectTemplate(
                                       object item, 
                                       DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;

        if (element == null || item == null)
            return null;

        if (item.GetType().Name.Contains("TestClass"))
            return element.FindResource("TestClassTemplate") as DataTemplate;

        // Check for other classes here...

        return null;
    }
}

and using it like

<!-- in ressources -->
<local:TestTemplateSelector x:Key="TestTemplateSelector" />

<ContentPresenter Content="{Binding MyPropertyContainingATestClass}"
                  ContentTemplateSelector="{StaticResource TestTemplateSelector}" />

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文