Data fields will not be displayed.

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 do not want to use the AutoGenerateFields property.

My problem is that the data form works fine when AutoGenerateFields is set to true, but when I create the DataForm and manually set the fields and run the application, all I get is an empty empty rectangle where my form should be and her fields.

I created an empty Silverligh Navigation application to test it, and below is the code for 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>

Code>

To make the second DataForm work, I simply created the Person class and placed 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 happened? Thank you in advance.

+3
source share
3 answers

In order for something to appear, I had to add:

        DataForm.CurrentItem = client;

to the code.

It simply displayed three text fields without labels and the entries "Test1", "Test2" and "Test3". Is that what you expected?

The Silverlight Toolkit sample page provides an example of a template-driven data form, and this 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 is a line:

        DataContext = Contact.People;

. ( People , )

+3

, - .

, :

 CurrentItem="{Binding Customer}"

- ,

 CurrentItem="{Binding}"

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

:

<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>
+1

You can specify CurrentItem = "" in xaml. Thus, you do not need to bind it to anything and at the same time work on the data form looks :)

0
source

Source: https://habr.com/ru/post/1728506/


All Articles