WPF在绑定XML时会有一些麻烦,经常是不显示列表内容,但是内容项却是存在,这说明配置XAML信息有误,见图例效果,绑定XML数据源,且能实现分组。
1.XML数据源,其中xmlns是命名空间,后面代码中会将其改成前缀名sb使用
- <?xmlversion="1.0"encoding="utf-8"?>
- <Studentsxmlns="http://sonny.com">
- <StudentStudentName="李金香"Sex="女"ClassID="4"/>
- <StudentStudentName="张亮"Sex="男"ClassID="7"/>
- <StudentStudentName="张利华"Sex="女"ClassID="9"/>
- <StudentStudentName="李頔飞"Sex="男"ClassID="10"/>
- <StudentStudentName="侯潇楠"Sex="女"ClassID="11"/>
- <StudentStudentName="杨超"Sex="男"ClassID="1"/>
- <StudentStudentName="陈磊"Sex="男"ClassID="2"/>
- <StudentStudentName="张涛"Sex="男"ClassID="3"/>
- <StudentStudentName="周立华"Sex="女"ClassID="4"/>
- <StudentStudentName="陈荔"Sex="女"ClassID="5"/>
- <StudentStudentName="赵欣环"Sex="男"ClassID="7"/>
- <StudentStudentName="黄博"Sex="男"ClassID="9"/>
- <StudentStudentName="苏三东"Sex="男"ClassID="10"/>
- <StudentStudentName="吴佳嵘"Sex="男"ClassID="11"/>
- <StudentStudentName="王立岩"Sex="女"ClassID="1"/>
- <StudentStudentName="季刚"Sex="女"ClassID="2"/>
- <StudentStudentName="李文达"Sex="男"ClassID="3"/>
- <StudentStudentName="高爱文"Sex="男"ClassID="4"/>
- <StudentStudentName="杨过"Sex="男"ClassID="9"/>
- <StudentStudentName="黄蓉"Sex="女"ClassID="3"/>
- <StudentStudentName="小新"Sex="男"ClassID="6"/>
- </Students>
- <Windowx:Class="Demo14_BindingFromXML.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:sys="clr-namespace:System;assembly=mscorlib"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:xm="clr-namespace:System.Xml;assembly=System.Xml"
- xmlns:local="clr-namespace:Demo14_BindingFromXML"
- Title="MainWindow"Height="333"Width="496">
- <Window.Resources>
- <XmlDataProviderx:Key="stuList"Source="Students.xml"
- XPath="/sb:Students/sb:Student">
- <XmlDataProvider.XmlNamespaceManager>
- <XmlNamespaceMappingCollection>
- <XmlNamespaceMappingUri="http://sonny.com"Prefix="sb"/>
- </XmlNamespaceMappingCollection>
- </XmlDataProvider.XmlNamespaceManager>
- </XmlDataProvider>
- <DataTemplatex:Key="showStudentTemplate">
- <TextBlock>
- <TextBlockText="{BindingXPath=@StudentName}"/>(<TextBlockText="{BindingXPath=@Sex}"/>:<TextBlockText="{BindingXPath=@ClassID}"/>)
- </TextBlock>
- </DataTemplate>
- </Window.Resources>
- <GridMargin="5"DataContext="{StaticResourcestuList}">
- <Grid.Resources>
- <StyleTargetType="TextBlock">
- <SetterProperty="Margin"Value="5"/>
- <SetterProperty="VerticalAlignment"Value="Center"/>
- </Style>
- <StyleTargetType="TextBox">
- <SetterProperty="Margin"Value="5"/>
- <SetterProperty="VerticalAlignment"Value="Center"/>
- </Style>
- <StyleTargetType="ListBox">
- <SetterProperty="Margin"Value="5"/>
- </Style>
- <StyleTargetType="Button">
- <SetterProperty="Margin"Value="5"/>
- <SetterProperty="Width"Value="65"/>
- <SetterProperty="Height"Value="25"/>
- <SetterProperty="HorizontalAlignment"Value="Left"/>
- </Style>
- </Grid.Resources>
- <Grid.RowDefinitions>
- <RowDefinition/>
- <RowDefinitionHeight="Auto"/>
- <RowDefinitionHeight="Auto"/>
- <RowDefinitionHeight="Auto"/>
- <RowDefinitionHeight="Auto"/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinitionWidth="80"/>
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <TextBlockText="List:"Grid.Row="0"Grid.Column="0"/>
- <TextBlockText="Name:"Grid.Row="1"Grid.Column="0"/>
- <TextBlockText="Gender:"Grid.Row="2"Grid.Column="0"/>
- <TextBlockText="Class:"Grid.Row="3"Grid.Column="0"/>
- <ListBoxGrid.Row="0"Grid.Column="1"ItemsSource="{Binding}"IsSynchronizedWithCurrentItem="True"
- ItemTemplate="{StaticResourceshowStudentTemplate}">
- <ListBox.GroupStyle>
- <GroupStyle>
- <GroupStyle.HeaderTemplate>
- <DataTemplate>
- <TextBlockBackground="Black"Foreground="White"FontWeight="Bold">
- <TextBlockText="{BindingXPath=@ClassID}"/>
- (<TextBlockText="{BindingItemCount}"/>)
- </TextBlock>
- </DataTemplate>
- </GroupStyle.HeaderTemplate>
- </GroupStyle>
- </ListBox.GroupStyle>
- </ListBox>
- <TextBoxGrid.Row="1"Grid.Column="1"Name="txtName"Text="{BindingXPath=@StudentName}"/>
- <TextBoxGrid.Row="2"Grid.Column="1"Name="txtGender"Text="{BindingXPath=@Sex}"/>
- <TextBoxGrid.Row="3"Grid.Column="1"Name="txtClass"Text="{BindingXPath=@ClassID}"/>
- <StackPanelOrientation="Horizontal"Grid.Row="4"Grid.Column="1">
- <ButtonContent="Group"Name="cmdGroup"Click="cmdGroup_Click"/>
- </StackPanel>
- </Grid>
- </Window>
- usingSystem.Windows;
- usingSystem.Windows.Data;
- usingSystem.ComponentModel;
- namespaceDemo14_BindingFromXML{
- ///<summary>
- ///MainWindow.xaml的交互逻辑
- ///</summary>
- publicpartialclassMainWindow:Window{
- publicMainWindow(){
- InitializeComponent();
- }
- privatevoidcmdGroup_Click(objectsender,RoutedEventArgse){
- ICollectionViewview=GetDefaultView();
- if(view.GroupDescriptions.Count==0){
- view.GroupDescriptions.Add(newPropertyGroupDescription("@ClassID"));
- }else{
- view.GroupDescriptions.Clear();
- }
- }
- privateICollectionViewGetDefaultView(){
- DataSourceProviderprovider=(DataSourceProvider)this.FindResource("stuList");
- varcollection=provider.Data;
- ICollectionViewview=CollectionViewSource.GetDefaultView(collection);
- returnview;
- }
- }
- }
注意:
1.DataTemplate定义显示方式,不再使用DataType属性,且绑定使用XPath,绑定的xml节点为xml的属性,故采用@属性名
2.在后置代码中获取数据源时
DataSourceProvider provider = (DataSourceProvider)this.FindResource("stuList");
var collection = provider.Data;
collection是MS.Internal.Data.XmlDataCollection,在PresentationFramework.dll程序集中,但是该命名空间下却无法看到该命名空间,该类声明是internal class XmlDataCollection,只能在它的命名空间中使用,所以外部无法使用。故我采用了var定义。
源代码下载:
http://u.163.com/1zTgkhv9
提取码:ojt73ata