【问题标题】:accessing specific textblock from a listbox on windows phone 8从 Windows Phone 8 上的列表框中访问特定的文本块
【发布时间】:2014-11-12 11:04:04
【问题描述】:

我有一个 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 个文本块和 5 个进度条,它们将获取文本块的值。我进行了很多搜索并尝试了很多东西,但我收到了各种错误和消息,而不是我想要的关于访问文本块的内容。

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

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

上面在文本块中给了我这个消息:

 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 个文本块,但我仍然无法从中获得价值来放置每个列表框我想要的进度条。

提前致谢。

【问题讨论】:

  • 为什么不直接在 XAML 上使用bind,从您的集合中加载信息?
  • @ReneSá 像这样直接在 XAML 上绑定 给我 null。如果它要给我一些东西,那会是什么?它如何知道何时何地放置每个值。正如我所说,有 5 个文本块来填充 5 个计数器,但此时要填充的计数器是 75。这就是为什么我写 Where(c => c.businessID == businessID && c.ratings == "5").ToString();
  • 你试过 List rating = (List)serializer.Deserialize(document.CreateReader());。我认为您在评级标签下有多个评级。然后使用简单的 linq 查询。
  • @DotNetWeblineindia 嗨,我把我的问题说得更具体了,这样你就可以更好地理解我想要做什么。
  • 我尝试过使用给定的 XML,但 XML 是错误的。你为什么拿了两次?你能粘贴实际的 XML 吗?

标签: c# windows-phone-8 data-binding textblock


【解决方案1】:

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

  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 并将该值放入进度条。

【讨论】:

    猜你喜欢
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多