简体   繁体   中英

accessing specific textblock from a listbox on windows phone 8

I have an xml file downloaded and saved to the phone first and then opened when user navigates to certain pages in the app. Now I already know how to view all the contents I want to show using a listbox and some textblocks bindings.

XML is like:

  <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>

I want to access the textblock that is inside the listbox, it is giving me up to 5 different values, and add them to specific ProgressBars. The page will be displaying up 5 Textblocks and 5 progressbars that will be getting the value of the Textblocks. I have searched a lot and tried a lot of things but I'm getting various errors and messages instead of what I want regarding accessing the textblock.

This is how I load the file.

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

This is how I can view the items at a listbox with some added arguments

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

And I want to do something like this.

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

The above gives me this message in the Textblock instead:

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

and something like this to get the textblock1 value and place it on the progressbar

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

I tried those unsuccessfully as well.

  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
                                      };

giving me almost the same message :

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

I have thought as well to bypass all this, not recommended but I'm desperate at this point, to make 5 Listboxes with 1 Textblock each, but I still can't get the value out of them to place each one on the progressbar that I want.

Thanks in advance.

I am always trying to do something in programming with the wrong and difficult approach when the answer was as simple as that.

  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;

That way I just pass the value into a var and then with a query I put the value to a Textblock and that value to a progress bar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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