Search code examples
c#xmlwindows-phone-8longlistselector

WP8 working with XML and LongListSelector


I'm trying to make a LongListSelector from xml file data.

public MainPage()
    {
        InitializeComponent();

        XDocument loadedData = XDocument.Load("/Resources/EuroTrip.xml");

        var data = from query in loadedData.Descendants("country")
                   select new Country
                   {
                       Name = (string)query.Element("name"),
                       IdentityCard = (string)query.Element("identityCard"),
                       CompulsoryDocuments = (string)query.Element("compulsoryDocuments"),
                       Regulations = (string)query.Element("regulations"),
                       Equipment = (string)query.Element("equipment"),
                       SpeedLimitsLightVehicles = (string)query.Element("speedLimitsLightVehicles"),
                       AutoClubs = (string)query.Element("autoClubs")
                   };
        countriesList.ItemsSource = data;
        // Set the data context of the listbox control to the sample data
        DataContext = App.ViewModel;
    }

    public class Country
{
    string name;
    string identityCard;
    string compulsoryDocuments;
    string regulations;
    string equipment;
    string speedLimitsLightVehicles;
    string autoClubs;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public string IdentityCard
    {
        get { return identityCard; }
        set { identityCard = value; }
    }

    public string CompulsoryDocuments
    {
        get { return compulsoryDocuments; }
        set { compulsoryDocuments = value; }
    }

    public string Regulations
    {
        get { return regulations; }
        set { regulations = value; }
    }

    public string Equipment
    {
        get { return equipment; }
        set { equipment = value; }
    }

    public string SpeedLimitsLightVehicles
    {
        get { return speedLimitsLightVehicles; }
        set { speedLimitsLightVehicles = value; }
    }

    public string AutoClubs
    {
        get { return autoClubs; }
        set { autoClubs = value; }
    }
    }

I used this tutorial: http://www.geekchamp.com/tips/wp7-working-with-xml-reading-filtering-and-databinding but I got an error on this line:

            countriesList.ItemsSource = data;

The error says:

An explicit conversion exists (are you missing a cast?)

I think because the LongListSelector in WP7 and WP8 aren't using the same controls, but I don't know what I must change and I don't find any useful article about this.

Thanks for your help:)

EDIT: this is the xaml code where I would want to bind the data:

            <!--Pivot item two-->
        <phone:PivotItem Header="Countries">
            <!--Double line list no text wrapping-->
            <phone:LongListSelector IsGroupingEnabled="False" x:Name="countriesList" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17">
                            <!--Image Width="60" Source="{Binding Photo}" Margin="12,6" HorizontalAlignment="Left"/-->
                            <TextBlock VerticalAlignment="Center" Text="{Binding LineOne}" TextWrapping="NoWrap" Margin="82,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}" Foreground="Black"/>
                            <TextBlock VerticalAlignment="Center" Text="{Binding LineTwo}" TextWrapping="NoWrap" Margin="82,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}" Foreground="Black"/>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </phone:PivotItem>

Solution

  • The LongListSelector can only take a list as data source. Just call the .ToList() method on your Linq query:

    countriesList.ItemsSource = data.ToList();
    

    Edit: To answer your comment, I'm not sure what you mean.

    You're binding the LongListSelector to "Items", so you need to expose your list of countries in the "Items" property of your viewmodel. Then, in the ItemTemplate of the LongListSelector, bind the controls to the properties of your Country class:

        <phone:PivotItem Header="Countries">
            <phone:LongListSelector IsGroupingEnabled="False" x:Name="countriesList" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                            <!-- Displays the name of the country -->
                            <TextBlock Text="{Binding Name}" />
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </phone:PivotItem>