views:

605

answers:

2
+2  A: 

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)

ChrisF
Yes, that's what I needed. Thank you very much :) I don't get why it's necessary to add that line in the code behind, though. Can't I create a blank form that isn't bound to anything? I just wanted to work on the looks of the dataform, so I thought I wouldn't have to set CurrentItem.
dsetton
A: 

I also found it quite surprising that you need to bind to something before the form will even appear.

If you're trying to bind to a single item you need to do this :

 CurrentItem="{Binding Customer}"

or - if you're in a user control, just

 CurrentItem="{Binding}"

and then in the parent control

<my:AddressControl DataContext="{Binding Customer}"/>

Here's the full dataform:

<dt:DataForm Name="dfAddress" AutoGenerateFields="False" CurrentItem="{Binding}">
    <dt:DataForm.EditTemplate>
        <DataTemplate>

            <StackPanel>
                <dt:DataField Label="First Name">
                    <TextBox Text="{Binding FirstName, Mode=TwoWay}" Style="{StaticResource FieldTextBoxStyle}" HorizontalAlignment="Stretch" IsReadOnly="False" HorizontalContentAlignment="Stretch" />
                </dt:DataField>
                <dt:DataField Label="Last Name">
                    <TextBox Text="{Binding LastName, Mode=TwoWay}" Style="{StaticResource FieldTextBoxStyle}" HorizontalContentAlignment="Stretch" />
                </dt:DataField>
             </StackPanel>
         </DataTemplate>
  </dt:DataForm.EditTemplate>
</dt:DataForm>
Simon_Weaver