aco
aco

Reputation: 829

Bind a Listbox from 2 tables

I am trying to bind a listbox from 2 tables. These 2 tables are related.

(windows phone project)

XAML:

<ListBox Name="LstOrders" ItemsSource="{Binding}" Margin="12,11,12,12" toolkit:TiltEffect.IsTiltEnabled="True" Height="643">
<ListBox.ItemTemplate>

    <DataTemplate>
                                    <StackPanel Margin="0">
                                        <TextBlock Text="{Binding refe}"
                                           Tag="{Binding idOrder}"
                                           Margin="0,0,0,0"
                                           FontSize="{StaticResource PhoneFontSizeExtraLarge}" 
                                           FontFamily="{StaticResource PhoneFontFamilySemiLight}"/>
                                        <TextBlock Text="{Binding tipo}"
                                           Margin="0,0,0,0"
                                           Foreground="{StaticResource PhoneSubtleBrush}"
                                           FontSize="{StaticResource PhoneFontSizeNormal}"/>
                                        <TextBlock Text="{Binding country}"
                                           Margin="0,0,0,0"
                                           Foreground="{StaticResource PhoneSubtleBrush}"
                                           FontSize="{StaticResource PhoneFontSizeNormal}"
                                           FontFamily="{StaticResource PhoneFontFamilySemiBold}" />
                                    </StackPanel>
                                </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

C#

EXAMPLE

using (ordersDBDataContext miDataContext = new ordersDBDataContext("Data Source='isostore:/mydatabase.sdf'"))
                {    
                    var _lista = from p in miDataContext.orders
                                     join t in miDataContext.tipos on p.idTipo equals t.idTipo 
                                     orderby p.refe
                                     where p.idCliente == _idCli
                                          select new 
                                          {
                                              p.refe, p.country,p.idOrder,t.tipo

                                          };

                    this.LstOrders.ItemsSource = _lista;                   
                }

RESULT

No display any data.What is wrong?

iF i doing this I can see that _lista contains correct data:

foreach (var rr in _lista)
{
       MessageBox.Show(rr.tipo);
}

Upvotes: 0

Views: 686

Answers (2)

dice
dice

Reputation: 2880

I don't know the context of your code snippet but I'm guessing you probably need to call

this.LstOrders.Items.Refresh();

I cant exactly explain why cos I am not familiar with WPF, but in winforms it would have been a DataBind().

Upvotes: 0

Phil
Phil

Reputation: 42991

You are disposing your DataContext (correctly), but this means when the _lista query is executed the DataContext will no longer be valid and there will be an exception. WPF unhelpfully swallows exceptions in certain circumstances so you probably aren't seeing the exception.

The solution is to use:

this.LstOrders.ItemsSource = _lista.ToList();

and also to either remove ItemsSource={Binding} from your xaml or alternatively leave the binding in and use

this.LstOrders.DataContext = _lista.ToList(); 

Also, see Silverlight 4 Data Binding with anonymous types which may be relevant to your problem.

Upvotes: 1

Related Questions