解析xml并将数据绑定到列表框

时间:2011-07-15 10:20:09

标签: windows-phone-7 listbox xml-parsing

这是一项非常简单的任务,但我奋斗了好几个小时。 我从Web源解析xml并将它们绑定到列表框。现在我想为绑定到列表框的每个项目创建一个索引,如下所示:

1.Title   2.Title   3.Title
  Author    Author    Author
  Date      Date      Date

这是我到目前为止所做的:

                <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Name="stkPnl"  Orientation="Horizontal" Margin="15,0" MouseEnter="stkPnl_MouseEnter" MouseLeave="stkPnl_MouseLeave">
                        <Image x:Name="imageAV" Source="{Binding avlink}" Height="80" Width="80" 
                               Stretch="UniformToFill" MouseLeftButtonUp="imageAV_MouseLeftButtonUp" ImageFailed="imageAV_ImageFailed"/>
                        <StackPanel Orientation="Vertical" Margin="10,0,0,0" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp">                                                               
                            <TextBlock Text="{Binding nickname}" Width="Auto" />                               
                            <TextBlock Text="{Binding track}" FontWeight="Bold" Width="Auto"/>
                            <TextBlock Text="{Binding artist}" Width="Auto"/>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>

和MainPage.xaml.cs

        private void DoWebClient()
    {
        var webClient = new WebClient();
        webClient.OpenReadAsync(new Uri("http://music.mobion.vn/api/v1/music/userstop?devid="));
        webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);

    }

    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        using (var reader = new StreamReader(e.Result))
        {

            string s = reader.ReadToEnd();
            Stream str = e.Result;
            str.Position = 0;
            XDocument xdoc = XDocument.Load(str);

            var data = from query in xdoc.Descendants("user")
                       select new mobion
                       {
                           avlink = (string)query.Element("user_info").Element("avlink"),
                           nickname = (string)query.Element("user_info").Element("nickname"),
                           track = (string)query.Element("track"),
                           artist = (string)query.Element("artist"),
                       };
            listBox.ItemsSource = data;
        }
    }

如你所见,我只有昵称,曲目和艺术家,如果我想为每个listboxItem添加一个增加的索引,我该怎么做? 感谢您阅读此问题。

2 个答案:

答案 0 :(得分:1)

我知道这很难看,但这是一个想法:为你的mobion类创建一个包装类:

public class mobionWrapper : mobion
{
    public int Index { get; set; }
}

您可以选择mobionWrapper实例,而不是选择mobion实例:

var data = from query in xdoc.Descendants("user")
            select new mobionWrapper
            {
                avlink = (string)query.Element("user_info").Element("avlink"),
                nickname = (string)query.Element("user_info").Element("nickname"),
                track = (string)query.Element("track"),
                artist = (string)query.Element("artist"),
            };

绑定数据后,设置包装类的Index属性:

listBox.ItemsSource = data;

for(int i = 0; i < listBox.Items.Count; i++)
{
    var item = listBox.Items[i] as mobionWrapper;
    item.Index = i + 1;
}

现在您需要一个新的TextBlock并将其绑定到Index属性:

<TextBlock Text="{Binding Index}" Width="Auto" />

为我工作。请记住,在对数据显示进行排序或过滤后,索引可能无效。

答案 1 :(得分:0)

假设你在mobion类(你可以在视图中绑定)中有一个合适的字段,你可以使用递增计数器来填充该字段,因为文档是迭代的。

int[] counter = { 1 };       

var data = from query in xdoc.Descendants("user")
           select new mobion
           {
               index = counter[0]++,
               avlink = (string)query.Element("user_info").Element("avlink"),
               nickname = (string)query.Element("user_info").Element("nickname"),
               track = (string)query.Element("track"),
               artist = (string)query.Element("artist"),
           };

请注意counter是一个int数组,而不仅仅是一个int,以防止访问修改后的闭包。