WPF XAML DesignTime Data - set property value on related model

115 Views Asked by At

Say we have a Person model:

public class Person
{
    public long Id { get; set; }
    public string Name { get; set; }
    public City City { get; set; }
}

And we have a City model:

public class City
{
    public long Id { get; set; }
    public long ZipCode { get; set; }
    public string Name { get; set; }
}

Then we have a WPF/XAML view like this:

<Window
x:Class="WpfIssues.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfIssues"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<ItemsControl Margin="20">
    <d:ItemsControl.ItemsSource>
        <x:Array Type="{x:Type local:Person}">
            <local:Person Name="Thomas" Id="1" />
        </x:Array>
    </d:ItemsControl.ItemsSource>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="20" />
                    <ColumnDefinition Width="150" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding Id}" />
                <TextBlock Grid.Column="1" Text="{Binding Name}" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

All that gives me a design-time view of the ItemsControl with a single Person item.

But I can't figure out how to set the design-time property values on the City property of the Person class - like ZipCode and City?

Anyone?

2

There are 2 best solutions below

0
On BEST ANSWER

you can use tag syntax:

<local:Person Name="Thomas" Id="1">
  <local:Person.City>
    <local:City Id="10" ZipCode ="zip"/>
  </local:Person.City>
</local:Person>

or attriute syntax with a resource (in general, it is less practical with design-time-only values)

<ItemsControl Margin="20">
    <ItemsControl.Resources>
        <local:City Id="10" ZipCode ="zip" x:Key="City10"/>
    </ItemsControl.Resources>
    <d:ItemsControl.ItemsSource>
        <x:Array Type="{x:Type local:Person}">
            <local:Person Name="Thomas" Id="1" City="{StaticResource City10}"/>
        </x:Array>
    </d:ItemsControl.ItemsSource>
0
On

It's typical - 10 secs after posting on SO - I find the solution:

<Window
x:Class="WpfIssues.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfIssues"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<ItemsControl Margin="20">
    <d:ItemsControl.ItemsSource>
        <x:Array Type="{x:Type local:Person}">
            <local:Person Name="Thomas" Id="1">
                <local:Person.City>
                    <local:City Name="Holte" />
                </local:Person.City>
            </local:Person>
        </x:Array>
    </d:ItemsControl.ItemsSource>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="20" />
                    <ColumnDefinition Width="150" />
                    <ColumnDefinition Width="150" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding Id}" />
                <TextBlock Grid.Column="1" Text="{Binding Name}" />
                <TextBlock Grid.Column="2" Text="{Binding City.Name}" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>