0

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>
ch40s
  • 580
  • 1
  • 7
  • 19

1 Answers1

1

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}})

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • thanks a lot, works like a charm :) i really start loving wpf ^^ – ch40s Oct 24 '11 at 09:34
  • OK that was too fast, now changes in xml are not shown instantly, but only when i reload it, updated question :) – ch40s Oct 24 '11 at 09:45
  • OK sry, nevermind. I changed the CollectionViewSource's Source to {Binding XPath=item}, and now it works :) thanks again – ch40s Oct 24 '11 at 09:57