views:

25

answers:

1

I'm currently investigating building a User Control in a Silverlight Project in Expression Blend 4. The control has an associated set of sample data for the User Control, and the data is appearing correctly in the User Control.

When I place the User Control on the main page, the sample data does not appear in the User Control. Is this correct behaviour, or am I setting/not setting something? What I am finding odd is that when I edit the User Control, the data appears in the Main Page alongside a Rebuild Indicator (Yellow Exclamation mark). When I rebuild, the data disappears again.

This is the Main Page Code:

<UserControl
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:SilverlightApplication2" mc:Ignorable="d"
x:Class="SilverlightApplication2.MainPage"
Width="1200" Height="640">
<UserControl.Resources>
    <local:MultiDayViewModel x:Key="MultiDayViewModelDataSource" d:IsDataSource="True"/>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White" d:DataContext="{d:DesignData /SampleData/TestSampleData.xaml}">
    <Grid.RowDefinitions>
        <RowDefinition Height="0.128*"/>
        <RowDefinition Height="0.872*"/>
    </Grid.RowDefinitions>
    <StackPanel Margin="0,24,8,8" HorizontalAlignment="Right" Width="318" Orientation="Horizontal">
        <Button Content="Daily"/>
        <Button Content="Weekly"/>
    </StackPanel>
    <local:MultiDayView x:Name="MultiDayView" Margin="8" Grid.Row="1" DataContext="{Binding Calenar, Source={StaticResource MultiDayViewModelDataSource}}"/>
</Grid>

Any thoughts or directions would be appreciated.

Thanks.

+2  A: 

You are using d:DataContext, which only appears in design mode. When you place the control inside MainPage, it is interpreted by Blend as being in runtime mode, so the data does not appear, so this is expected behavior.

When you create the sample data for you control in Blend you can specify whether you want this sample data to be used during runtime as well, or you can simply set the DataContext property instead of or in addition to the d:DataContext property.

The following image shows how you can enable sample data during runtime, when you create the sample data source from Blend:

Enable sample data when application is running

When you select the option called "Enable sample data when application is running", your XAML looks like this:

    <UserControl
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="ASD_Answer002.MainPage"
        Width="640" Height="480">
        <UserControl.Resources>
            <DataTemplate x:Key="DataTemplate1">
                <CheckBox Content="{Binding Property1}" IsChecked="{Binding Property2, Mode=TwoWay}"/>
            </DataTemplate>
        </UserControl.Resources>

        <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
            <ItemsControl ItemsSource="{Binding Collection}" ItemTemplate="{StaticResource DataTemplate1}" Margin="50"/>
        </Grid>
    </UserControl>

This will show your sample data for both design time and runtime.

Murven
Thank you, Murven, I did try this approach, but the compiler did not like the DataContext reference as "DesignData" is not in the Blend 2008 namespace.
Jason
That is correct, I have updated my answer to include how the XAML looks like when you use the option in Blend that enables sample data during runtime.
Murven
I also noticed you are declaring an instance of local:MultiDayViewModel. You could make that instance return mock data for your properties and this would show data both in design and runtime, if you set both DataContext and d:DataContext to point to that view model instance. This is more useful if you create a ViewModelLocator that returns a mock view model during design time and your real view model during runtime.
Murven