将 DataGrid 与 XmlDataProvider 源分组

发布于 2024-12-11 14:56:48 字数 2476 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄖ落Θ余辉 2024-12-18 14:56:48

这是一个示例,应该是不言自明的,但如果有不清楚的地方,请随时询问:

  <DataGrid>
    <DataGrid.Resources>
        <CollectionViewSource x:Key="items" Source="{Binding SelectedItem, ElementName=lb}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="@name"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </DataGrid.Resources>
    <DataGrid.ItemsSource>
        <Binding Source="{StaticResource items}"/>
    </DataGrid.ItemsSource>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding XPath=@value}"/>
    </DataGrid.Columns>
    <DataGrid.GroupStyle>
        <GroupStyle />
    </DataGrid.GroupStyle>
  </DataGrid>

(您也可以设置 IsSynchronizedWithCurrentItemtrueListBox 上,然后通过当前项绑定 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:

  <DataGrid>
    <DataGrid.Resources>
        <CollectionViewSource x:Key="items" Source="{Binding SelectedItem, ElementName=lb}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="@name"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </DataGrid.Resources>
    <DataGrid.ItemsSource>
        <Binding Source="{StaticResource items}"/>
    </DataGrid.ItemsSource>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding XPath=@value}"/>
    </DataGrid.Columns>
    <DataGrid.GroupStyle>
        <GroupStyle />
    </DataGrid.GroupStyle>
  </DataGrid>

(You can also set IsSynchronizedWithCurrentItem to true on the ListBox and then bind the Source via the current item instead (i.e. {Binding /, Source={StaticResource data}})

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文