将 DataGrid 与 XmlDataProvider 源分组
我有一个 XmlDataProvider、一个 ListBox 和一个 DataGrid。 底层 xml 文件具有这种结构:
<Root>
<Person name="test">
<item name="bla" value="test"/>
<item name="bla" value="test2"/>
</Person>
<Root>
ListBox 列出所有人员,而 DataGrid 列出与所选人员相对应的所有项目。这按预期工作。 现在我想对 DataGrid 中的数据进行分组,但是查看了示例后,我仍然不知道如何使用 XmlDataProvider 来实现这一点(如何/在哪里从 XmlDataProvider 创建 ListCollectionView)。 有人可以给我一个快速的 xaml 示例来执行此操作,例如按名称对项目进行分组吗?:) 感谢您提前提供任何帮助:)
问候
更新: 现在分组可以工作,但是当我向 xml 添加某些内容时,它不再立即显示(在列表框或数据网格中)。出了什么问题?我对 wpf 真的很陌生,所以可能会有多余或不必要的东西,我对你指出它们没有任何问题:) 这是使用的相关代码:
<Grid.DataContext>
<XmlDataProvider x:Name="XmlData" Source="entries.xml" XPath="Root/Person" />
</Grid.DataContext>
<ListBox Name="PersonListBox"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource listBoxTemplate}"
IsSynchronizedWithCurrentItem="True"
Visibility="Visible" SelectionMode="Single" SelectedIndex="-1" DataContext="{Binding}">
</ListBox>
<DataGrid IsSynchronizedWithCurrentItem="True" Name="itemGrid"
DataContext="{Binding ElementName=PersonListBox, Path=SelectedItem}"
CanUserAddRows="true"
IsReadOnly="true"
AutoGenerateColumns="False">
<DataGrid.Resources>
<CollectionViewSource x:Key="items" Source="{Binding XPath=item}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@name"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</DataGrid.Resources>
<DataGrid.ItemsSource>
<Binding Source="{StaticResource items}"/>
</DataGrid.ItemsSource>
<DataGrid.Columns>
<DataGridTextColumn Width="*" Header="Name" Binding="{Binding XPath=@name}"/>
<DataGridTextColumn Header="Wert" Binding="{Binding XPath=@value}"/>
</DataGrid.Columns>
<DataGrid.GroupStyle>
<GroupStyle />
</DataGrid.GroupStyle>
</DataGrid>
I have a XmlDataProvider, a ListBox and a DataGrid.
The underlying xml file has this kind of structure:
<Root>
<Person name="test">
<item name="bla" value="test"/>
<item name="bla" value="test2"/>
</Person>
<Root>
The ListBox lists all persons, while the DataGrid lists all items, corresponding to the selected Person. This works as intended.
Now i want to group the data in the DataGrid, but having looked at examples i still don't get how to do it with an XmlDataProvider (how/where to create a ListCollectionView off of the XmlDataProvider).
Could someone please give me a quick xaml example for doing this by e.g grouping the items by name?:)
Thanks for any help in advance :)
regards
UPDATE:
Now the grouping works, but when i add something to the xml, it is not shown instantly anymore (in listbox or datagrid).What is wrong? I am really new to wpf, so there might be things redundant or unnecessary, i got no problems with you pointing them out :)
Here is the relevant code that is used:
<Grid.DataContext>
<XmlDataProvider x:Name="XmlData" Source="entries.xml" XPath="Root/Person" />
</Grid.DataContext>
<ListBox Name="PersonListBox"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource listBoxTemplate}"
IsSynchronizedWithCurrentItem="True"
Visibility="Visible" SelectionMode="Single" SelectedIndex="-1" DataContext="{Binding}">
</ListBox>
<DataGrid IsSynchronizedWithCurrentItem="True" Name="itemGrid"
DataContext="{Binding ElementName=PersonListBox, Path=SelectedItem}"
CanUserAddRows="true"
IsReadOnly="true"
AutoGenerateColumns="False">
<DataGrid.Resources>
<CollectionViewSource x:Key="items" Source="{Binding XPath=item}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="@name"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</DataGrid.Resources>
<DataGrid.ItemsSource>
<Binding Source="{StaticResource items}"/>
</DataGrid.ItemsSource>
<DataGrid.Columns>
<DataGridTextColumn Width="*" Header="Name" Binding="{Binding XPath=@name}"/>
<DataGridTextColumn Header="Wert" Binding="{Binding XPath=@value}"/>
</DataGrid.Columns>
<DataGrid.GroupStyle>
<GroupStyle />
</DataGrid.GroupStyle>
</DataGrid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个示例,应该是不言自明的,但如果有不清楚的地方,请随时询问:
(您也可以设置
IsSynchronizedWithCurrentItem
为true
在ListBox
上,然后通过当前项绑定Source
(即{Binding /, Source={StaticResource data}}
) EM>Here is an example, should be quite self-explanatory but if something is not clear feel free to ask:
(You can also set
IsSynchronizedWithCurrentItem
totrue
on theListBox
and then bind theSource
via the current item instead (i.e.{Binding /, Source={StaticResource data}}
)