I have an entity framework with a many-to-many relationship between Customers and Contacts.
I have generated a Domain Service Class and added the following method manually.
public Customer GetCustomerById(int Id)
{
return this.ObjectContext.Customer.Include("Contacts").SingleOrDefault(s => s.Id == Id);
}
I now want to create a page that shows me the customer details and a list of contacts associated with that customer.
I have the following in codebehind of the customerdetails.xaml to read the Id parameter that gets passed into the page.
public int CustomerId
{
get { return (int)this.GetValue(CustomerIdProperty); }
set { this.SetValue(CustomerIdProperty, value); }
}
public static DependencyProperty CustomerIdProperty = DependencyProperty.Register("CustomerId", typeof(int), typeof(CustomerDetails), new PropertyMetadata(0));
// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (this.NavigationContext.QueryString.ContainsKey("Id"))
{
CustomerId = Convert.ToInt32(this.NavigationContext.QueryString["Id"]);
}
}
I use the following xaml for the page:
<Grid x:Name="LayoutRoot" DataContext="{Binding ElementName=customerByIdSource, Path=Data}">
<riaControls:DomainDataSource Name="customerByIdSource" AutoLoad="True" QueryName="GetCustomerById">
<riaControls:DomainDataSource.QueryParameters>
<riaControls:Parameter ParameterName="Id" Value="{Binding ElementName=CustomerDetailsPage, Path=CustomerId}" />
</riaControls:DomainDataSource.QueryParameters>
<riaControls:DomainDataSource.DomainContext>
<sprint:Customer2DomainContext/>
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
<StackPanel x:Name="CustomerInfo" Orientation="Vertical">
<StackPanel Orientation="Horizontal" Margin="3,3,3,3">
<TextBlock Text="Id"/>
<TextBox x:Name="idTextBox" Text="{Binding Id}" Width="160"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="3,3,3,3">
<TextBlock Text="Name"/>
<TextBox x:Name="nameTextBox" Text="{Binding Name}" Width="160"/>
</StackPanel>
<ListBox ItemsSource="{Binding Contact}" DisplayMemberPath="FullName" Height="100" />
</StackPanel>
</Grid>
When I do this the textboxes get nicely populated through the databinding, but the listbox remains empty.
Two questions:
Can I somehow specify the return type of the GetCustomerById query, so I can see the names when I specify the binding through the properties GUI?
What am I doing wrong here? Why isn't my ListBox populated? Am I going about this the correct way or do I need to set the databinding for the listbox in codebehind as well? If so, how? I haven't found how toaccess the Contacts property via the domain data source programmatically.
I use silverlight and entity framework 4.