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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
hfrmobile
  • 1,213
  • 16
  • 16
  • i think you can only do that by Assigning FallBackValue – eran otzap Oct 05 '12 at 00:42
  • The easiest way to generate design time data is to use Blend. http://visualstudiomagazine.com/articles/2012/07/12/design-time-data-for-windows-phone.aspx and a follow up article: http://visualstudiomagazine.com/articles/2012/07/23/disable-design-time-data.aspx – Metro Smurf Oct 05 '12 at 13:53

1 Answers1

1

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>
JYL
  • 8,228
  • 5
  • 39
  • 63
  • Sorry for the confusion, using MVVM pattern and the design-time-data is the ViewModel of course (PersonViewModelSampleData.xaml). Didn't add the PersonViewModel C# code here since it is nothing special (implements all properties which I want to display and it implements INotifyPropertyChanged). {Binding Path=Employer.Name} works fine for runtime. – hfrmobile Oct 07 '12 at 09:12
  • YES, you did it! Forgot to add the namespace for the "domain model" (also forgot a default ctor for the Company domain item which is necessary for the Designer). Thanks a lot! – hfrmobile Oct 08 '12 at 19:48