1

私は、listboxすべての行に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>

とはどこBookですかprivate static List<BookItems> Book{ get; set; }BookItems

    public struct BookItems
    {
        public string Name{ get; set; }
        public string Url { get; set; }
        public string Author{ get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public string Tag { get; set; }
        public ImageSource ImageSource { get; set; }
    }

渡したいすべてのデータListBoxは別の場所に保存されていますListprivate static List<Tuple<StringBuilder , StringBuilder , StringBuilder , StringBuilder , StringBuilder >> BookList = new List<Tuple<StringBuilder , StringBuilder , StringBuilder , StringBuilder , StringBuilder >>();

だから私ListBoxがそのように埋めようとすると:

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

それから私nullは画像以外のどこにでも行きます。BookListtoと Book も変更するstringsと、すべてが問題なくスムーズに進みます。StringBuilderからへの変換をstringコンソール出力で毎回チェックしstringsますが、私の StringBuilder は問題ありません。私は何か間違ったことをしていますか?

4

2 に答える 2

0

あなたが言ったように、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クラスにします。これらをすべて実行すると、デバッグ プロセスは簡単になります。にデータが含まれている場合stringBuildersは、データに何が起こるかを正確に確認できます。

于 2013-07-10T10:09:21.427 に答える