简体   繁体   中英

ListBox in C# doesn't show up

I already have a ListBox in my Code and now I added a new one:

 <ListBox x:Name="Diaryresult"
                             Foreground="Black"
                             Margin="19,0,0,8">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Binding {name}"
                                               FontSize="24" />
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

I am populating this list with following code:

   XElement diary = XElement.Parse(e.Result);
                IEnumerable<XElement> diaryelements = diary.Elements("diaryelement");

                List<Produkt> diaryprodukte = new List<Produkt>();

                foreach (XElement diaryelement in diaryelements)
                {
                    Produkt p = new Produkt();
                    p.name = diaryelement.Element("diaryshortitem").Element("description").Element("name").Value;
                    p.shortfacts = diaryelement.Element("diaryshortitem").Element("data").Element("kj").Value + " KJ - "
                            + diaryelement.Element("diaryshortitem").Element("data").Element("kcal").Value + "kcal";
                    diary.Add(p);

                    Debug.WriteLine("Added "+p.name);
                }
                Diaryresult.ItemsSource = diaryprodukte;


                Diaryresult.Visibility = System.Windows.Visibility.Visible;

But, it doesn't show up. Does anyone see the trick?

Your binding tag isn't correct. "Binding {Name}" doesn't means anything for XAML. {Binding Name} means to databind the property Name of your context which is what you're trying to do.

Replace:

<TextBlock Text="Binding {name}" FontSize="24" />

With:

<TextBlock Text="{Binding name}" FontSize="24" />

Also you need to add the element to the list:

dairyprodukt.Add(p);

And, remember to call your NotifyPropertyChanged () once done in order to notify the UI Thread of the changes. I mean, you're using Diaryresult.Visibility = System.Windows.Visibility.Visible; is this your way to notify your UI, are you using MVVM or CodeBehind?

It doesn't look like you are adding your Produkt to dairyprodukte. dairyprodukte is still an empty list when you bind it.

try

foreach (XElement diaryelement in diaryelements)
            {
                Produkt p = new Produkt();
                p.name = diaryelement.Element("diaryshortitem").Element("description").Element("name").Value;
                p.shortfacts = diaryelement.Element("diaryshortitem").Element("data").Element("kj").Value + " KJ - "
                        + diaryelement.Element("diaryshortitem").Element("data").Element("kcal").Value + "kcal";
                diary.Add(p);

                Debug.WriteLine("Added "+p.name);
                diaryprodukte.Add(p);
            }

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