views:

452

answers:

1

I'm looking for a workaround for my RIA Services project, which has a Listbox with the Listitems as a user control defined as an ItemTemplate, like this:

<ListBox x:Name="lstMain">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel Orientation="Horizontal">
           <foo:ListItemDetail />
       </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

The Listbox is bound to a Domain Service in the code-behind. The List items are of type ListItemDetail, which has editable fields and an Update button. I'd like to call the Domain Service (the parent Lists's Data Context) to SaveChanges() and update the item when the button is clicked, but there is no way to get to the (original) Domain Service that populated the list. Is there an elegant solution for this?

I am trying to use the Listbox tag item to store the domain context, but I am unable to get to it from a ListItem. I get the parent StackPanel, but it's parent is null.

Thanks in advance, Ra

+1  A: 

You can create a property on the ListItemDetail control. Then you can bind that property to the ListBox's DataContext

<ListBox x:Name="lstMain">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel Orientation="Horizontal">
           <foo:ListItemDetail MyRiaContext="{Binding DataContext, ElementName=lstMain}"/>
       </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>
NotDan