0

Using design-time data for my Windows Phone Apps which works fine for string, int etc. (e.g. here: Person name, Person age) but when I like to do that for "nested object" (e.g. here: Company/Employer) I have no idea how to do this in the design-time-data-XAML file.

Company:

public class Company
{
  public string Name { get; set; }
  public int Size { get; set; }
}

Person:

public class Person
{
  public int Age { get; set; }
  public string Name { get; set; }
  public Company Employer { get; set; }
}

PersonViewModel.cs:

"Normal" ViewModel which implements INotifyPropertyChanged and has properties for all data I want to display.

PersonViewModelSampleData.xaml:

<local:PersonViewModel 
    xmlns="http:schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http:schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Hfr.BlaBla.ViewModels"

    Name="Harald-René Flasch"
    Age="36">
</local:PersonViewModel>

Person Page XAML:

<TextBlock
    Text="{Binding Path=Employer.Name}"
    Style="{StaticResource PhoneTextLargeStyle}"
    TextWrapping="Wrap" ... />

So, Path=Employer.Name works fine at run-time but I have no idea how to provide that data for design-time support. Any suggestions?

4

1 回答 1

1

我不理解您的示例数据:它将是一个实例Person或一个实例PersonViewModel(但在这种情况下,viewModel 应该具有类型属性CompanyPerson两者兼有)。

如果您的示例数据是 Person 的一个实例:

<local:Person
    xmlns="http:schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http:schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Hfr.BlaBla.MyModelsNamespace"

    Name="Harald-René Flasch"
    Age="36">
     <local:Person.Employer>
             <local:Company Name="MyCompany"/>
     </local:Person.Employer>
</local:Person>

注意命名空间:这里的“本地”xmlns 指的是模型命名空间(而不是 viewModel)。

编辑: 如果您的示例数据是 viewModel,假设您的 ViewModel 作为Employer具有 setter(不仅是 getter)的属性,类型为Company

<local:PersonViewModel 
    xmlns="http:schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http:schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Hfr.BlaBla.ViewModels"
    xmlns:myModel="clr-namespace:Hfr.BlaBla.MyModelsNamespace"
    Name="Harald-René Flasch"
    Age="36">
    <local:PersonViewModel.Employer>
            <myModel:Company Name="MyCompany"/>
    </local:PersonViewModel.Employer>
</local:PersonViewModel>
于 2012-10-05T13:47:32.423 回答