开发者

Can't bind child collection in silverlight

I have a pretty simple setup that I cannot get to work in silverlight. I have an order with a collection of OrderPayments. These objects are part of a Entity Framework model and are exposed through WCF RIA Services. I can bind perfectly fine to any basic property on the Order class, but I wanted to bind to a listbox to show the OrderPayments. Here's the XAML for the ListBox.

<ListBox ItemsSource="{Binding Data.OrderPayments, ElementName=orderDataSource}"></ListBox>

Nothing ever appears in the listbox and there is at least one OrderPayment for the order displayed. orderDataSource is a DomainDataSource that contains the Order. The odd thing about this is that I don't receive any binding errors and when I bind to a TextBlock using the following code:

<TextBlock Text="{Binding Data.OrderPayments, ElementName=orderDataSource}" />

The text 'OrderPayment' is outputted to the screen. Is there something different I have to do to get Silverlight to actually pick up the reference to the object?

Entire XAML below:

<riaControls:DomainDataSource AutoLoad="True" Name="orderDataSource" QueryName="GetOrder">
        <riaControls:DomainDataSource.QueryParameters>
            <riaControls:Parameter ParameterName="orderid" Value="1" />
        </riaControls:DomainDataSource.QueryParameters>
        <riaControls:DomainDataSource.DomainContext>
            <ds:CEWCPSDomainContext />
        </riaControls:DomainDataSource.DomainContext>
    </riaControls:DomainDataSource>

    <StackPanel Orientation="Vertical" Margin="12">
        <StackPanel Orientation="Horizontal">
            <Button Width="100" Height="50" Content="Save &amp; Return" Margin="0,0,12,12" />
            <Button Width="100" Height="50" Content="Orders" Margin="0,0,12,12" />
            <Button Width="100" Height="50" Content="Emails" Margin="0,0,12,12" />
        </StackPanel>

        <StackPanel Orientation="Horizontal">
            <dataForm:DataForm x:Name="dataForm1" Header="Order Contact Information" 
                       AutoGenerateFields="False" AutoEdit="False" AutoCommit="False" 
                      CurrentItem="{Binding Data, ElementName=orderDataSource}">
                <dataForm:DataForm.EditTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <StackPanel>
                        <dataForm:DataField Label="First Name">
                            <TextBox Text="{Binding FirstName, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }" />
                        </dataForm:DataField>
                        <dataForm:DataField Label="Last Name">
                            <TextBox Text="{Binding LastName, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
                        </dataForm:DataField>
                        <dataForm:DataField Label="Organization">
                            <TextBox Text="{Binding Organization, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
                        </dataForm:DataField>
                        <dataForm:DataField Label="Phone">
                            <TextBox Text="{Binding Phone, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
                        </dataForm:DataField>
                        <dataForm:DataField Label="Fax">
                            <TextBox Text="{Binding Fax, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
                        </dataForm:DataField>
                        <dataForm:DataField Label="Email">
                            <TextBox Text="{Binding Email, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
                        </dataForm:DataField>
                        <dataForm:DataField Label="Address 1">
                            <TextBox Text="{Binding Address1, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
                        </dataForm:DataField>
                        <dataForm:DataField Label="Address 2">
                            <TextBox Text="{Binding Address2, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
                        </dataForm:DataField>
                        <dataForm:DataField Label="City">
                            <TextBox Text="{Binding City, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
                        </dataForm:DataField>
                        <dataForm:DataField Label="State">
                            <TextBox Text="{Binding State, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
                        </dataForm:DataField>
                        <StackPanel Orientation="Horizontal" Width="Auto">
                            <dataForm:DataField Label="Zip code">
                                <TextBox Text="{Binding Zip, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
                            </dataForm:DataField>
                            <dataForm:DataField>
                                <TextBox Text="{Binding Zip4, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
                            </dataForm:DataField>
                        </StackPanel>
                        <dataForm:DataField Label="Country">
                            <TextBox Text="{Binding Country, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
                        </dataForm:DataField>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </dataForm:DataForm.EditTemplate>
            </dataForm:DataForm>

            <StackPanel Orientation="Vertical">
                <TextBlock FontWeight="Bold" FontSize="16" Text="Order Total / Payments" />
                <TextBlock >
                    <Run Text="Order Total:" />
                    <Run Text="{Binding Data.OrderTotal, ElementName=orderDataSource}" />
                </TextBlock>开发者_运维问答;
                <TextBlock Text="Payments" />
                <TextBlock Text="{Binding Data.OrderPaymentItems, ElementName=orderDataSource}" />
                <ListBox ItemsSource="{Binding Data.OrderPayments, ElementName=orderDataSource}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Amount}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

            </StackPanel>
        </StackPanel>
    </StackPanel>


WCF RIA Services does not include child entities by default. I needed to put [Include()] on the OrderPayments property of the Order object. Works like a charm now.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜