繁体   English   中英

从Windows Phone 8的列表框中访问特定的文本块

[英]accessing specific textblock from a listbox on windows phone 8

我有一个xml文件已下载并首先保存到手机中,然后在用户导航到应用程序中的某些页面时打开。 现在,我已经知道如何使用列表框和一些文本块绑定查看要显示的所有内容。

XML就像:

  <root>
    <ratings>
      <rating>
        <ratings/>
        <businessID/>
        <counters/>
      </rating>
     ...
    </ratings>
  </root>

XAML

   <ListBox x:Name="sI" 
                 Foreground="Sienna" Margin="368,283,61,-144" RenderTransformOrigin="0.5,0.5">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock x:Name="textblock1" Text="{Binding counter} FontSize="30"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
    </ListBox>

我想访问列表框内的文本块,它最多可以给我5个不同的值,并将它们添加到特定的ProgressBars中。 该页面将显示5个Textblock和5个进度栏,这些进度条将获取Textblock的值。 我已经搜索了很多并尝试了很多东西,但是我遇到了各种各样的错误和消息,而不是我想要访问的文本块。

这就是我加载文件的方式。

  XmlSerializer serializer = new
                    XmlSerializer(typeof(Ratings));
                    XDocument document = XDocument.Load(isoStream);
                    Ratings ratings = (Ratings)serializer.Deserialize(document.CreateReader());

这是我可以在列表框中查看带有附加参数的项目的方法

 sI.ItemsSource = ratings.Collection.Where(c => c.businessID == businessID).ToList();

我想做这样的事情。

 textblock1.DataContext/Text = ratings.Collection.Where(c => c.businessID == businessID && c.ratings == "4").ToString();

上面在Textblock中给了我这个消息:

 System.Linq.Enumerable+WhereEnumerableIterator`1[Name_of_the_solution.Rating]

和类似的东西来获取textblock1值并将其放在进度栏上

 progressbar4.Value = Double.Parse(textblock1.Text);

我也尝试了失败。

  var elem = from element in document.Elements("ratings")
                              where (string)element.Attribute("rating") == "2" &&
                              (string)element.Attribute("businessID") == businessID
                              select elements().Element("counter");

 var filteredData = from c in document.Descendants("Rating")
                                      where c.Attribute("rating").Value == "1" && c.Attribute("businessID").Value == businessID.ToString()
                                     select new Rating()
                                      {
                                          counter = c.Attribute("counter").Value
                                      };

给我几乎相同的信息:

 System.Linq.Enumerable+WhereEnumerableIterator`2[Name_of_the_solution.Rating]

我也曾想过绕过所有这些,不建议这样做,但我现在很拼命,要制作5个具有1个Textblock的列表框,但是我仍然无法从中获得价值来将每个列表框放在进度条上我想要。

提前致谢。

当答案就这么简单的时候,我总是试图用错误和困难的方法来做一些编程。

  XmlSerializer serializer = new
                    XmlSerializer(typeof(Ratings));
                    XDocument document = XDocument.Load(isoStream);
                    Ratings ratings = (Ratings)serializer.Deserialize(document.CreateReader());

                    var result = ratings.Collection.Where(c => c.businessID == businessID);
                    if (result == null) return;
                    double db = 0.0;

                    ficounter.Text = result.Where(c => c.ratings == 5).Select(c => c.counter).SingleOrDefault();
                    if (double.TryParse(ficounter.Text, out db))
                        fiprogress.Value = db;

                    focounter.Text = result.Where(c => c.ratings == 4).Select(c => c.counter).SingleOrDefault();
                    if (double.TryParse(focounter.Text, out db))
                        foprogress.Value = db;

                    thcounter.Text = result.Where(c => c.ratings == 3).Select(c => c.counter).SingleOrDefault();
                    if (double.TryParse(thcounter.Text, out db))
                        thprogress.Value = db;

                    twcounter.Text = result.Where(c => c.ratings == 2).Select(c => c.counter).SingleOrDefault();
                    if (double.TryParse(twcounter.Text, out db))
                        twprogress.Value = db;

                    oncounter.Text = result.Where(c => c.ratings == 1).Select(c => c.counter).SingleOrDefault();
                    if (double.TryParse(oncounter.Text, out db))
                        onprogress.Value = db;

这样,我只是将值传递给var,然后通过查询将值传递给Textblock并将该值传递给进度条。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM