Search code examples
c#wpfwindows-phonedesign-time-data

Design time data


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?


Solution

  • I don't understand your sample data : it would be an instance of Person OR an instance of PersonViewModel (but in that case the viewModel should have a property of type Company or Person or both).

    If your sample data is an instance of 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>
    

    Be carefull of namespaces : here the "local" xmlns refers to the model namespace (not the viewModel).

    EDIT : If your sample data is the viewModel, assuming that your ViewModel as a property Employer with a setter (not only a getter), of type 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>