1
votes

I have the following constellation in my XAML:

<ListBox x:Uid="attributesListBox" 
    DataContext="{Binding Source={StaticResource EditFeatureWithForeignKeyAttributesViewModel}}" ItemsSource="{Binding Path=Attributes}" 
    d:DataContext="{d:DesignData /SampleData/BlendableAttributesSampleData.xaml}">
</ListBox>

The following DataTemplate is applied via the DataType:

<DataTemplate x:Uid="dataColumnFeatureAttributeDataTemplate" DataType="{x:Type DataColumnFeatureAttribute}">
    <!-- ... opens a button and a grid ... -->
            <TextBlock Text="{Binding EditCaption}" />
    <!-- ... closes button and grid ... -->
</DataTemplate>

As explained here in the "Remarks" section, an x:Key is generated implicitly and "the DataTemplate gets applied automatically to data objects of that type".

Inside the DataTemplate, several simple properties of the list item are bound. In this excerpt from my SampleData, you will find the EditCaption property referenced above - it's a simple string, and the DataContext and ItemsSource of the ListBox should fit:

<SampleData:BlendableAttributes xmlns:SampleData="clr-namespace:Commons.Mobile.SampleData">
    <SampleData:BlendableAttributes.Attributes>
        <Mobile_Client:FeatureAttribute DisplayCaption="Vestibulum adipiscing curabitur" DisplayString="Class maecenas parturient" ErrorMessage="Nunc amet aliquam nam sed" EditCaption="Cras phasellus duis" IsEditable="True" IsSearchable="False" IsViewable="True" Modified="False">
            <Mobile_Client:FeatureAttribute.Feature>
                <!-- ... Feature with a lot of properties ...  -->
            </Mobile_Client:FeatureAttribute.Feature>
        </Mobile_Client:FeatureAttribute>
        <!-- ... more FeatureAttributes ... -->
    </SampleData:BlendableAttributes.Attributes>
</SampleData:BlendableAttributes>

The DataTemplate is applied correctly in the preview, if I reference it via x:Key. This is not possible in this case, because the list contains different types of objects, so I have to use the implicit x:Key to apply a specific DataTemplate according to the object type.

So there's no problem at runtime with the data from my VM, but at design time

  • Expression Blend shows the SampleData, but without applying the DataTemplate when I look at the whole page
  • On the other hand, when I go to Resources and edit the DataTemplate, I see the layout without the SampleData.

I've tried some things out and looked for a solution in the forum, but no one seems to have had this special constellation. Can someone help me please?

1

1 Answers

0
votes

Did you configure the build action DesignData for the file "/SampleData/BlendableAttributesSampleData.xaml"? If yes, the Visual Studio designer does not create an object of type DataColumnFeatureAttribute for your sample data, but instead a proxy object of type _.di0.DataColumnFeatureAttribute (or something similar). Therefore, your DataTemplate is not used, because the DataType property of your DataTemplate refers to another type than the proxy type.

You might change the build action to DesignDataWithDesignTimeCreatableTypes to fix that problem (this requires that the DataColumnFeatureAttribute class has a default constructor).

Alternatively, if you cannot use DesignDataWithDesignTimeCreatableTypes, then set the property d:IsDesignTimeCreatable to False in your DataTemplate, like this:

<DataTemplate x:Uid="dataColumnFeatureAttributeDataTemplate" 
              DataType="{x:Type DataColumnFeatureAttribute}" 
              d:IsDesignTimeCreatable="False"> 
    <!-- ... opens a button and a grid ... --> 
            <TextBlock Text="{Binding EditCaption}" /> 
    <!-- ... closes button and grid ... --> 
</DataTemplate>