开发者

Dataform fields won't appear

开发者 https://www.devze.com 2022-12-16 05:14 出处:网络
I am trying to learn how to use the Silverlight 3 DataForm cont开发者_如何学Gorol, because I need to define the DataForm fields myself in the XAML code, that is, I don\'t want to use the AutoGenerateF

I am trying to learn how to use the Silverlight 3 DataForm cont开发者_如何学Gorol, 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:

Dataform fields won't appear

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


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)


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>


You could give the CurrentItem="" in xaml. In this way, you dont need to actual bind it to anything and at the same time, work on the dataform looks :)

0

精彩评论

暂无评论...
验证码 换一张
取 消