Turkwise
Turkwise

Reputation: 147

WPF DataGrid ComboBox load in XML data

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>

Upvotes: 1

Views: 1375

Answers (1)

d.moncada
d.moncada

Reputation: 17402

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>

Upvotes: 1

Related Questions