我想将我的xml binded datagrid分组......
这段代码我从互联网上获得了工作! 但是我没有将DataContext绑定到列表(addressList),而是需要在这里绑定我的XML文件... 我该怎么做?
addressList = new ObservableCollection<Address>(){ <== this is an example ! But I've got a XML file ?! Howto ?
new Address{ Country="Argentina", City="Buenos Aires", Street="Ldiary"},
new Address{ Country="Argentina", City="Buenos Aires", Street="Translations"},
new Address{ Country="Austria", City="Graz", Street="Ldiary Translations"},
new Address{ Country="Austria", City="Salzburg", Street="Ldiary"},
new Address{ Country="Belgium", City="Bruxelles", Street="Translations"},
new Address{ Country="Belgium", City="Charleroi", Street="Ldiary Translations"},
new Address{ Country="Brazil", City="Campinas", Street="Japanese"},
new Address{ Country="Brazil", City="Resende", Street="English"},
new Address{ Country="Brazil", City="Rio de Janeiro", Street="Ldiary Translations"},
new Address{ Country="Canada", City="Montreal", Street="Ldiary Translations"},
new Address{ Country="Canada", City="Montreal", Street="Ldiary Translations"},
new Address{ Country="Denmark", City="Arhus", Street="English to Japanese"},
new Address{ Country="Denmark", City="Kobenhavn", Street="Ldiary Translations"},
new Address{ Country="Finland", City="Helsinki", Street="Ldiary Translations"},
new Address{ Country="Finland", City="Oulu", Street="Ldiary Translations"},
new Address{ Country="France", City="Lille", Street="Ldiary Translations"},
new Address{ Country="France", City="Lyon", Street="Ldiary Translations"},
new Address{ Country="France", City="Marseille", Street="Ldiary Translations"},
new Address{ Country="France", City="Nantes", Street="English"},
new Address{ Country="Germany", City="Aachen", Street="Japanese"},
new Address{ Country="Germany", City="Berlin", Street="Ldiary Translations"}
};
this.DataContext = addressList; <= Not
CollectionView cv = CollectionViewSource.GetDefaultView(dataGrid.DataContext) as CollectionView;
cv.GroupDescriptions.Add(new PropertyGroupDescription("Country"));
我的XAML:
<Window.Resources>
<XmlDataProvider x:Key="SmsData" XPath="/response"> => source added by code
</XmlDataProvider>
<local:RowColorConverter x:Key="RowColorConverter" />
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Source={StaticResource SmsData}, XPath=conv/sms}" AutoGenerateColumns="False" Name="dataGrid1" GridLinesVisibility="None" IsReadOnly="True" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" Margin="-5,13,4,244" BorderThickness="0" UseLayoutRounding="False" IsEnabled="True" SelectionChanged="dataGrid1_SelectionChanged">
<DataGrid.GroupStyle>
<GroupStyle AlternationCount="6" >
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ItemsPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.RowStyle>
<Style>
<Setter Property="DataGridRow.Background"
Value="{Binding RelativeSource={RelativeSource AncestorType=GroupItem},
Path=(ItemsControl.AlternationIndex), Converter={StaticResource RowColorConverter}}"/>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding XPath=msgid}" Width="100" />
<DataGridTextColumn Binding="{Binding XPath=imsgid}" Width="100" />
<DataGridTextColumn Binding="{Binding XPath=recipient}" Width="100" />
<DataGridTextColumn Binding="{Binding XPath=datetime}" Width="120" />
<DataGridTextColumn Binding="{Binding XPath=message}" Width="225" />
</DataGrid.Columns>
</DataGrid>
</Grid>
在cs中(字段msgid是要分组的元素):
XmlDataProvider dataProvider = this.FindResource("SmsData") as XmlDataProvider;
dataProvider.Source = new Uri(someurl);
DataContext = this;
CollectionView cv = CollectionViewSource.GetDefaultView(dataGrid1.DataContext) as CollectionView;
cv.GroupDescriptions.Add(new PropertyGroupDescription("msgid"));
我的xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<response>
<conv>
<sms><recipient>test</recipient><datetime/><message/></sms>
<sms>
<msgid>123456</msgid>
<datetime>15-12-2012 21:36:55</datetime>
<message>test</message>
</sms>
</conv>
</response>
答案 0 :(得分:1)
您可以直接在Xaml中绑定XML数据。
示例:
在Window或Control资源
中从您的文件创建一个xml源代码 <Window.Resources>
<XmlDataProvider x:Key="AddressXML" XPath="/Address" Source="C:\AddressData.xml" />
</Window.Resources>
然后,您可以将文档中的元素/节点绑定到DataGrid或其他控件。
<Grid>
<DataGrid DataContext="{StaticResource AddressXML}" ItemsSource="{Binding XPath=Address}" />
</Grid>
如果您发布了xml文件的示例,我可以为您提供更好的解释
xaml中的CollectionViewSource
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:dat="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
<Window.Resources>
<XmlDataProvider x:Key="SmsData" XPath="/response" Source="C:\data.xml"/>
<CollectionViewSource x:Key="SmsView" Source="{StaticResource SmsData}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="datetime" />
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<dat:PropertyGroupDescription PropertyName="msgid" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<DataGrid DataContext="{StaticResource SmsView}" ItemsSource="{Binding XPath=conv/sms}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding XPath=msgid}" Width="100" />
<DataGridTextColumn Binding="{Binding XPath=imsgid}" Width="100" />
<DataGridTextColumn Binding="{Binding XPath=recipient}" Width="100" />
<DataGridTextColumn Binding="{Binding XPath=datetime}" Width="120" />
<DataGridTextColumn Binding="{Binding XPath=message}" Width="225" />
</DataGrid.Columns>
</DataGrid>
</Grid>