1

I'm having trouble setting XML data as the ItemsSource for my DataGrid Combobox.

Below is my XML code:

<?xml version="1.0" standalone="yes"?>
<Table>
    <FRUIT>
        <edible>True</edible>
        <Types main="Apple">
            <Type>Fuji</Type>
            <Type>Gala</Type>
        </Types>
    </FRUIT>
    <FRUIT>
        <edible>True</edible>
        <Types main="Banana">
            <Type>Burro</Type>
            <Type>Red</Type>
        </Types>
    </FRUIT>
</Table>

Next is the XAML code for the WPFtoolkit DataGrid Combobox:

<Custom:DataGridTemplateColumn Header="Fruits" Width="300">
    <Custom:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox Name="cboFruit"/>
        </DataTemplate>
    </Custom:DataGridTemplateColumn.CellTemplate>
</Custom:DataGridTemplateColumn>

Before I had to load the data to a ComboBox, I just put the XML data into a DataSet and set the DataGrid's DataContext to the first table in the DataSet.

Now that won't work since I'm trying to put each type of the fruits into the ComboBox.

I can change anything in the XML to get it to work. Let me know if you need more info.

Thanks in advance!

Posted below is the full XAML for my CustomDataGrid (WPFtoolkit):

<Window.DataContext>
    <XmlDataProvider x:Name="FruitData" XPath="fruits/fruit" />
</Window.DataContext>
<Grid>
    <Custom:CustomDataGrid x:Name="dgFruits" AutoGenerateColumns="False" Margin="5" CanUserAddRows="True"
                          ItemsSource="{Binding XPath=fruits/fruit}"><!--Here is confusion-->
        <Custom:DataGrid.Columns>
            <!--Edible-->
            <Custom:DataGridTextColumn Header="Edible" Binding="{Binding XPath=edible}"/>
            <!--Fruit-->                
            <Custom:DataGridTemplateColumn Header="Fruit Types" Width="300">
                <Custom:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>                            
                        <ComboBox ItemsSource="{Binding XPath=types/type}"/><!--This works fine on a combobox outside of the DataGrid-->
                    </DataTemplate>
                </Custom:DataGridTemplateColumn.CellTemplate>
            </Custom:DataGridTemplateColumn>                
    </Custom:CustomDataGrid>
</Grid>
Turkwise
  • 137
  • 3
  • 17
  • 1
    your XML file is invalid. A XML document can have only 1 root. Also we don't need any DataSet to load the XML. You can use such as `XmlDataProvider`, or even Linq-to-XML. – King King Oct 21 '14 at 19:52
  • Ah, my bad. I do have 1 root to en capsule the FRUITs (named ), I forgot to include. Will the XMLDataProvider allow for a dynamic source?
    – Turkwise Oct 21 '14 at 20:27
  • I'm not really sure but the `Source` property is changeable. You can just try changing that property in code behind. Note that the structure of data should be the same so that the current Binding won't be broken after switching the Source. – King King Oct 21 '14 at 20:36

1 Answers1

1

Here's an example using XmlDataProvider

Update XML layout to the following:

<?xml version="1.0" standalone="yes"?>
<fruits xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <fruit>
    <edible>True</edible>
    <types main="Apple">
      <type>
        <name>Fuji</name>
      </type>
      <type>
        <name>Gala</name>
      </type>
    </types>
  </fruit>
  <fruit>
    <edible>True</edible>
    <types main="Banana">
      <type>
        <name>Burro</name>
      </type>
      <type>
        <name>Red</name>
      </type>
    </types>
  </fruit>
</fruits>

In xaml, create the Data and then access XML nodes using XPath

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <XmlDataProvider x:Name="FruitData" Source="fruits.xml" XPath="fruits/fruit" />      
    </Window.DataContext>
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="FruitDataTemplate">
                <StackPanel>
                    <Label Content="{Binding XPath=edible}"/>
                    <ComboBox ItemsSource="{Binding XPath=types/type}"/>
                </StackPanel>
            </DataTemplate>
        </Grid.Resources>
        <ListBox ItemsSource="{Binding}"
                 ItemTemplate="{StaticResource FruitDataTemplate}"
                 IsSynchronizedWithCurrentItem="True"
                 Visibility="Visible" SelectionMode="Single">
        </ListBox>
    </Grid>
</Window>

Edit:

Here it is using a DataGrid

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <XmlDataProvider x:Name="FruitData" Source="fruits.xml"  />      
    </Window.DataContext>
    <Grid>
        <DataGrid x:Name="dgFruits" AutoGenerateColumns="False" Margin="5" CanUserAddRows="True"
                       ItemsSource="{Binding XPath=fruits/fruit}">
            <DataGrid.Columns>
                <!--Edible-->
                <DataGridTextColumn Header="Edible" Binding="{Binding XPath=edible}"/>
                <!--Fruit-->
                <DataGridTemplateColumn Header="Fruit Types" Width="300">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding XPath=types/type}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • Ah, thank you so much for your response! Quick question - will the XMLDataProvider work if the Source XML file path changes? I mean say I have a C:\test1\fruits.xml and C:\test2\fruits.xml. The user will be able to choose between the two XML files. – Turkwise Oct 21 '14 at 20:22
  • 1
    @Turkwise you can definitely change the source. See this post for more info: http://stackoverflow.com/questions/2037906/how-to-change-xmldataprovider-source-in-wpf-at-runtime – d.moncada Oct 21 '14 at 21:20
  • Thank you, I got the info to populate in a regular combobox, but the combobox in my CustomDataGrid remains blank when I use the same code. Is there something I'm missing in the main part of the DataGrid? I tried but was a no-go. – Turkwise Oct 21 '14 at 21:46
  • @Turkwise post your current XAML so i can take a look – d.moncada Oct 21 '14 at 21:48
  • Okay, I added the XAML to the bottom of the post. Thanks! – Turkwise Oct 21 '14 at 22:03
  • 1
    See my Edit. it had to do with the XPath defining of the XmlDataProvider. My new code works, as expected. – d.moncada Oct 21 '14 at 22:14
  • That works wonderful! You've been very helpful. Thanks so much! – Turkwise Oct 21 '14 at 22:56