WP7 DesignData与自定义对象

时间:2012-05-03 13:53:46

标签: windows-phone-7 xaml data-binding

我正在开发Windows Phone 7.0的应用程序,我正在尝试添加一个示例设计数据文件,以便在使用visual studio时获得图形。 字符串或int类型工作正常,但自定义对象没有。

我解释说:

我有一个这样的类:(代表我的自定义对象)

public class CustomObject
{
    public string Title { get; set; }
} 

这是我的观点模型:

public class MainViewModel
{
    public MainViewModel()
    {
        this.Custom= new CustomObject();
        this.Custom.Title = "Hey, i'm the Runtime Custom";
    }

    public CustomObject Custom{ get; set; }

    public string TestProperty { get; set; }
}

我创建了一个这样的示例数据文件:

<local:MainViewModel 
    xmlns:local="clr-namespace:Project.ViewModels"
    TestProperty ="I'm the Design testProperty and I work">

    <local:MainViewModel.Custom Title="Hey, i'm the Design Custom, and I don't work" />

</local:MainViewModel>

在我的应用程序的主页面中,我已将其添加到手机标记中:

d:DataContext="{d:DesignData SampleData.xaml}"

好吧,当我使用TestProperty变量(Text="{Binding TestProperty}")进行测试时,它工作正常,但是当我尝试使用我的对象(Text="{Binding Custom.Title}")时,它不起作用..

我找到的所有资源都不能谈论自定义对象。

有人有任何想法吗?

坦克。

PS:我尝试添加DesignData标签(最能描述我的问题),但我不允许:(

编辑: 没有编译,visual studio不显示任何内容,当我编译时,它显示Hey, i'm the Runtime Custom ...它绕过我的示例数据文件,但仅限于此自定义对象。

2 个答案:

答案 0 :(得分:1)

试试这个:

<local:MainViewModel 
    xmlns:local="clr-namespace:Project.ViewModels"
    TestProperty ="I'm the Design testProperty and I work"
    xmlns:local2="clr-namespace:Project.NamespaceDefinitionOfTheCustomClass">

    <local:MainViewModel.Custom>
        <local2:Custom Title="Hey, i'm the Design Custom, and I don't work" />
    </local:MainViewModel.Custom>

</local:MainViewModel>

如果你的类“Custom”没有在“MainViewModel”类的同一名称空间中定义,那么就像我一样添加名称空间声明,否则只删除它并将local2作用域重命名为local;

它对我有用。希望这对你有用。

答案 1 :(得分:0)

尝试稍微简单一些:

<local:MainViewModel 
    xmlns:local="clr-namespace:Project.ViewModels"
    TestProperty ="I'm the Design testProperty and I work">

    <local:Custom Title="Hey, i'm the Design Custom, and I don't work" />
</local:MainViewModel>
相关问题