C# C在ListboxItems内容中传递StringBuilder

C# C在ListboxItems内容中传递StringBuilder,c#,listbox,type-conversion,stringbuilder,C#,Listbox,Type Conversion,Stringbuilder,我有一个列表框,我在其中每行传递5个项目。我的XML文件如下所示: <ListBox x:Name="DatabaseBox" ItemsSource="{Binding Book}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" Width="auto" Height="22">

我有一个列表框,我在其中每行传递5个项目。我的XML文件如下所示:

 <ListBox x:Name="DatabaseBox" ItemsSource="{Binding Book}">
     <ListBox.ItemTemplate>
        <DataTemplate>
          <StackPanel Orientation="Horizontal" Width="auto" Height="22">
             <Image x:Name="ToggleFavoriteImage" Width="10" Height="10" Tag="{Binding Tag}" Source="{Binding ImageSource}" HorizontalAlignment="Center"/>
                <TextBlock Text="{Binding Name}" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Center"/>
                <ListBoxItem Content="{Binding City}" HorizontalAlignment="Center"/>
                <ListBoxItem Content="{Binding Author}" HorizontalAlignment="Center"/>
                <ListBoxItem Content="{Binding Country}" HorizontalAlignment="Center"/>
          </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
 </ListBox>
我想在ListBox中传递的所有数据都存储在另一个列表中:private static List BookList=new List

因此,如果我尝试以这种方式填充我的列表框:

 foreach(var ListItem in BookList)
 {
    Book= new List<BookItems>() 
    {
      new BookItems() 
      {
         Tag = ListItem.Item1.ToString(),ImageSource = FavoriteSource, Name= ListItem.Item1.ToString(), Author= ListItem.Item3.ToString(), City = ListItem.Item4.ToString(),Country = ListItem.Item5.ToString()
       }
     };
     DatabaseBox.Items.Add(Book);
 }

然后,除了图像,我到处都是空的。如果我把我的书单改成字符串,把我的书也改成字符串,那么一切都会顺利进行,没有任何问题。每次使用控制台输出从StringBuilder到字符串的转换时,我都会检查字符串是否为空,此时StringBuilder正常。我做错什么了吗

您应该在元组中为StringBuilder项创建实例,就像新建StringBuilder一样

正如您所说,StringBuilder很好,因此我的解决方案是首先不要为每个项创建列表。我不知道你为什么这么做,但不应该为你添加到DatabaseBox的每一项都列出一个列表

第二,用你的代码

Book= new List<BookItems>() 
{
  new BookItems() 
  {
     Tag = ListItem.Item1.ToString(),ImageSource = FavoriteSource, Name= ListItem.Item1.ToString(), Author= ListItem.Item3.ToString(), City = ListItem.Item4.ToString(),Country = ListItem.Item5.ToString()
   }
 };
然后重写它。还要重写BookItems中的属性并使其成为类。
完成所有这些之后,调试过程将变得简单。如果StringBuilder包含数据,您将确切地看到它发生了什么

不,在这一个编译器错误中。但我为什么要做新的StringBuilder??我很好地填写了我的StringBuilder列表。为什么对元组的每个部分都使用“StringBuilder”以及如何获取数据?这是我大学的一个项目,要求使用StringBuilder。至于抓取,我从一个XML文件中读取,并使用BookList.AddTuple.Createx1、x2、x3、x4、x5传递它们;其中x1,x2。。。是临时StringBuilders。你试过调试它吗?看到从xml中获取值了吗?你能告诉我问题从哪里开始吗?问题从StringBuilder-ToString的转换开始。我很好地填写了stringbuilder列表,问题在于转换。
Book= new List<BookItems>() 
{
  new BookItems() 
  {
     Tag = ListItem.Item1.ToString(),ImageSource = FavoriteSource, Name= ListItem.Item1.ToString(), Author= ListItem.Item3.ToString(), City = ListItem.Item4.ToString(),Country = ListItem.Item5.ToString()
   }
 };