Question

Hi.... Could anyone please let me know how to bind windows phone 7 list box item data template my problem is that i am getting the data properly in the collection object and also i m assigning it to the item source property but still i am not able to see any data into it am i missing something please check the below design code and cs code

-- Design code

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

                <ListBox Name="lstAromaticsPrices" Margin="0,131,0,0" ItemsSource="{Binding}" 
                         VerticalAlignment="Top" Width="476"
                         Loaded="lstAromaticsPrices_Loaded" Grid.Row="0" Visibility="Visible">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <Grid x:Name="Price1" Grid.Row="1" Margin="12,0,12,0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="150*"></RowDefinition>


                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="75*"></ColumnDefinition>
                                        <ColumnDefinition Width="65"></ColumnDefinition>
                                        <ColumnDefinition Width="55"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>

                                    <TextBlock Text="{Binding Path=category}"  FontSize="20"></TextBlock>
                                    <TextBlock Text="{Binding Path=price}"></TextBlock>

                                </Grid>
                            </StackPanel>


                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>

--- cs code below

  private void FillPricesData()
    {
        WebClient client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompletedEvent);
        client.DownloadStringAsync(new Uri("http://www.test.com/web/Prices.asmx/getdDat?Username=test1&Password=test123"));

    }

    private void DownloadStringCompletedEvent(object sender, DownloadStringCompletedEventArgs e)
    {

            if (e.Error != null)
                return;
            XElement element = XElement.Parse(e.Result.ToString());

            List<Prices> prices = (from p in element.Descendants("Table")
                                   orderby (string)p.Element("category")
                                   select new Prices
                                   {
                                       category = (string)p.Element("category"),
                                       price = (string)p.Element("price"),
                                       //priceDate = (string)p.Element("priceDate")
                                   }).ToList<Prices>();

            lstAromaticsPrices.ItemsSource = prices;

    }


    private void lstAromaticsPrices_Loaded(object sender, RoutedEventArgs e)
    {

        FillPricesData();

    }
Was it helpful?

Solution

your Prices class should look like this:

public class Prices
{
    public string category { get; set; }
    public string price { get; set; }
}

pay attention at the public in front of the class

OTHER TIPS

Remove ItemSource and listbox loaded event from xaml:

<ListBox Name="lstAromaticsPrices" Margin="0,131,0,0" 
                         VerticalAlignment="Top" Width="476"
                          Grid.Row="0" Visibility="Visible">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>                           
                                    <TextBlock Text="{Binding Path=category}"  FontSize="20"></TextBlock>
                                    <TextBlock Text="{Binding Path=price}"></TextBlock>                      
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

In code behind:

 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
            {
              FillPricesData();
            }
            base.OnNavigatedTo(e);
        }

 private void FillPricesData()
    {
        WebClient client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompletedEvent);
        client.DownloadStringAsync(new Uri("http://www.test.com/web/Prices.asmx/getdDat?Username=test1&Password=test123"));

    }

    private void DownloadStringCompletedEvent(object sender, DownloadStringCompletedEventArgs e)
    {

            if (e.Error != null)
                return;
            XElement element = XElement.Parse(e.Result.ToString());

            List<Prices> prices = (from p in element.Descendants("Table")
                                   orderby (string)p.Element("category")
                                   select new Prices
                                   {
                                       category = (string)p.Element("category"),
                                       price = (string)p.Element("price"),
                                       //priceDate = (string)p.Element("priceDate")
                                   }).ToList<Prices>();

            lstAromaticsPrices.ItemsSource = prices;

    }

In the XAML ItemTemplate

 <StackPanel>
    <Grid  Margin="12,0,12,0">
       <Grid.ColumnDefinitions>
            <ColumnDefinition Width="75*"></ColumnDefinition>
            <ColumnDefinition Width="65"></ColumnDefinition>
            <ColumnDefinition Width="55"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock Text="{Binding category}"  FontSize="20"/>
        <TextBlock Grid.Column="1" Text="{Binding price}"/>
    </Grid>
</StackPanel>

Code Behind DownloadStringCompleted Method:

if (e.Error != null)
{
   lstAromaticsPrices.ItemsSource = 
      new List<Prices>
      {
          new Prices()
          {
              category = "No Categories available",
              price = "0.00$"
          },
          new Prices()
          {
              category = "No Prices available",
              price = "0.00€"
          }
      };
   return;
}
XElement element = XElement.Parse(e.Result.ToString());
List<Prices> prices = (from p in element.Descendants("Table")
                           orderby (string)p.Element("category")
                           select new Prices
                           {
                               category = p.Element("category").Value,
                               price = p.Element("price").Value,
                               //priceDate = (string)p.Element("priceDate")
                           }).ToList<Prices>();
//lstAromaticsPrices.ItemsSource = null;
if(prices.Any())
    lstAromaticsPrices.ItemsSource = prices;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top