C# 如何在WPF列表框项中显示列表框绑定到的XML文件中的节点数量?

C# 如何在WPF列表框项中显示列表框绑定到的XML文件中的节点数量?,c#,xml,wpf,listbox,C#,Xml,Wpf,Listbox,我在C#上编写了一个WPF应用程序。那里有一个XML文件“Book.XML”。此文件显示在下面 <?xml version="1.0" encoding="utf-8" ?> <Books xmlns=""> <Category name="Computer Programming"> <Book> <Author>H. Schildt</Author> <Title>C# 4

我在C#上编写了一个WPF应用程序。那里有一个XML文件“Book.XML”。此文件显示在下面

<?xml version="1.0" encoding="utf-8" ?>
<Books xmlns="">
  <Category name="Computer Programming">
    <Book>
      <Author>H. Schildt</Author>
      <Title>C# 4.0 The Complete Reference</Title>
    </Book>
  </Category>
  <Category name="Art Editions">
    <Book>
      <Author>M. Cervantes</Author>
      <Title>The Ingenious Gentleman Don Quixote of La Mancha </Title>
    </Book>
    <Book>
      <Author>P. Ronsard</Author>
      <Title>Les Amours</Title>
    </Book>
  </Category>
</Books>

但我不知道怎么做。我将衷心感谢你的帮助。请。

如果最终要在代码隐藏中手动解析xml文件,那么最好在模型上创建并将列表框绑定到其集合

public class CategoryModel
{
    public string Name { get; set; }
    public int BookCount { get; set; }
}

public MainWindow()
{
    BookCategories = new ObservableCollection<CategoryModel>();
    var doc = XDocument.Parse("path_to_Books.xml");
     // Loop through all <Category> nodes.
     foreach (var category in doc.Root.Elements("Category"))
     {
          // Count <Book> nodes within current <Category> node.
            var numberOfBooks = category.Elements("Book").Count();
           // Save the calculated quantity in the list.
             BookCategories.Add(new CategoryModel { Name = category.Attribute("name").Value, BookCount = numberOfBooks });
      }

      InitializeComponent();
}



 public ObservableCollection<CategoryModel> BookCategories { get; set; }
公共类类别模型
{
公共字符串名称{get;set;}
public int BookCount{get;set;}
}
公共主窗口()
{
BookCategories=新的ObservableCollection();
var doc=XDocument.Parse(“path_to_Books.xml”);
//循环遍历所有节点。
foreach(doc.Root.Elements(“类别”)中的变量类别)
{
//对当前节点中的节点进行计数。
var numberOfBooks=category.Elements(“Book”).Count();
//将计算的数量保存在列表中。
添加(新CategoryModel{Name=category.Attribute(“Name”).Value,BookCount=numberOfBooks});
}
初始化组件();
}
公共ObservableCollection图书类别{get;set;}
在Xaml中:

<ListBox Name="lbxCategory" Grid.Row="0" Grid.Column="0" 
             ItemsSource="{Binding BookCategories }"

             IsSynchronizedWithCurrentItem="true"
             SelectionChanged="lbxCategory_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}"></TextBlock>
                        <Label Content="("></Label>
                        <Label Content="{Binding BookCount}"></Label>
                        <Label Content=")"></Label>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

谢谢你,尼特。你帮了我。
ComputerProgramming   (1)
Art Editions          (2)
public class CategoryModel
{
    public string Name { get; set; }
    public int BookCount { get; set; }
}

public MainWindow()
{
    BookCategories = new ObservableCollection<CategoryModel>();
    var doc = XDocument.Parse("path_to_Books.xml");
     // Loop through all <Category> nodes.
     foreach (var category in doc.Root.Elements("Category"))
     {
          // Count <Book> nodes within current <Category> node.
            var numberOfBooks = category.Elements("Book").Count();
           // Save the calculated quantity in the list.
             BookCategories.Add(new CategoryModel { Name = category.Attribute("name").Value, BookCount = numberOfBooks });
      }

      InitializeComponent();
}



 public ObservableCollection<CategoryModel> BookCategories { get; set; }
<ListBox Name="lbxCategory" Grid.Row="0" Grid.Column="0" 
             ItemsSource="{Binding BookCategories }"

             IsSynchronizedWithCurrentItem="true"
             SelectionChanged="lbxCategory_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}"></TextBlock>
                        <Label Content="("></Label>
                        <Label Content="{Binding BookCount}"></Label>
                        <Label Content=")"></Label>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>