R – Dataform fields won’t appear

dataformsilverlightsilverlight-3.0

I am trying to learn how to use the Silverlight 3 DataForm control, because I need to define the DataForm fields myself in the XAML code, that is, I don't want to use the AutoGenerateFields property.

My problem is: the dataform works perfectly when the AutoGenerateFields is set to true, but when I create a DataForm and set the fields manually and run the application, all I get is an empty blank rectangle where my form and its fields should be.

I created a blank Silverligh Navigation Application to test this, and below is the code of the Home.xaml page:


<Grid x:Name="LayoutRoot">

    <StackPanel>

        <!-- This doesn't work. It renders a blank rectangle -->
        <dataFormToolkit:DataForm x:Name="DataForm">
            <dataFormToolkit:DataForm.EditTemplate>
                <DataTemplate>
                    <StackPanel dataFormToolkit:DataField.IsFieldGroup="True">
                        <dataFormToolkit:DataField>
                            <TextBox Text="Test1" />
                        </dataFormToolkit:DataField>
                        <dataFormToolkit:DataField>
                            <TextBox Text="Test2" />
                        </dataFormToolkit:DataField>
                        <dataFormToolkit:DataField>
                            <TextBox Text="Test3" />
                        </dataFormToolkit:DataField>
                    </StackPanel>
                </DataTemplate>
            </dataFormToolkit:DataForm.EditTemplate>
        </dataFormToolkit:DataForm>

        <!-- This works. -->
        <dataFormToolkit:DataForm x:Name="DataForm2"/>

    </StackPanel>

</Grid>


To make the second DataForm work, I simply created a Person class, and put the following in Home.xaml.cs:


protected override void OnNavigatedTo(NavigationEventArgs e)
{
    Person client = new Person { Age = 10, DateOfBirth = new DateTime(1980, 10, 20), FirstName = "John", LastName = "Doe" };
    DataForm2.CurrentItem = client;
}

You can see what happens when I run the application:

screenshot

Does anyone know what's wrong? Thank you in advance.

Best Answer

To get something to appear I had to add:

        DataForm.CurrentItem = client;

to the code.

This just displayed three text boxes with no labels and the entries "Test1", "Test2" and "Test3". Is this what you were expecting?

The Silverlight Toolkit samples page has an example of a template driven data form and it's XAML looks like this:

        <dataform:DataForm x:Name="dataForm" ItemsSource="{Binding}" HorizontalAlignment="Left" MinWidth="400" MaxWidth="500" Margin="4" Grid.Column="1">
            <dataform:DataForm.EditTemplate>
                <DataTemplate>
                    <StackPanel>
                        <dataform:DataField>
                            <TextBox Text="{Binding FirstName, Mode=TwoWay}" />
                        </dataform:DataField>
                        <dataform:DataField>
                            <TextBox Text="{Binding Email, Mode=TwoWay}" />
                        </dataform:DataField>
                        <dataform:DataField>
                            <TextBox Text="{Binding Phone, Mode=TwoWay}" />
                        </dataform:DataField>
                        <dataform:DataField Label="Calendar">
                            <controls:Calendar></controls:Calendar>
                        </dataform:DataField>
                    </StackPanel>
                </DataTemplate>
            </dataform:DataForm.EditTemplate>
        </dataform:DataForm>

And there's the line:

        DataContext = Contact.People;

in the code behind. (The class People is defined elsewhere as far as I can work out)

Related Topic