0

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);
}
svick
  • 236,525
  • 50
  • 385
  • 514
aco
  • 819
  • 4
  • 15
  • 32
  • What you can see in quick watch for _lista.ToList()? Is there any data there? – Saeed Amiri Mar 03 '12 at 17:02
  • Hi Saeed: _lista contains the correct data. (now I have added more info in my question). – aco Mar 03 '12 at 17:14
  • If you are right by `this.LstOrders.ItemsSource = _lista.ToList();` this should be solve. – Saeed Amiri Mar 03 '12 at 17:18
  • same problem. But I have seeen that only runs if I execute the basic query (only 1 table): from p in miDataContext.orders orderby p.refe where p.idCliente == _idCli select p ...... – aco Mar 03 '12 at 20:04
  • Hi, I have similar problem with you. It do get the items, but when it tried to enter the page, it give me error and out of the application. I tried add assembly and all your solution, is there anything you missed to show ? – Nich Apr 23 '13 at 15:32

2 Answers2

1

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.

Community
  • 1
  • 1
Phil
  • 42,255
  • 9
  • 100
  • 100
  • Hi Phil: I have tried this but display no data. But I have seen that _lista contains data but is not binded – aco Mar 03 '12 at 17:15
  • Can we see the items control that's using the DataTemplate? – Phil Mar 03 '12 at 17:23
  • I don't see anything wrong. You can try removing the ItemsSource={Binding} since you're explicitly setting ItemsSource. Also try removing the TiltEffect in case there's something strange going on. I would have used ItemsSource={Binding} if I had set LstOrders.DataContext= _lista.ToList(); – Phil Mar 03 '12 at 19:34
  • same result. no data. But now I have seen that if I do this basic query, then listbox display data: from p in miDataContext.orders orderby p.refe where p.idCliente == _idCli select p – aco Mar 03 '12 at 19:41
  • perhaps WP7 can't handle binding to anonymous types. Try specifying the type, as in 'select new Order{ refe=p.refe, ... };' – Phil Mar 03 '12 at 19:51
  • Thanks Phil. Now runs fine. Solution is in the link that you have posted :) – aco Mar 05 '12 at 10:42
0

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().

dice
  • 2,820
  • 1
  • 23
  • 34