Click here to Skip to main content
16,009,728 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have used one XMLfile as data source for my datagrid in a WPF.

XML:-
XML
<?xml version="1.0" encoding="utf-8" ?>
<Profiles>
  <Profile Name="PGB570/120/600/120" Weight="19200" Bolt="42"/>
  <Profile Name="PGB570/100/600/100" Weight="16400" Bolt="42"/>
  <Profile Name="PGB570/70/600/70" Weight="13300" Bolt="36"/>
  <Profile Name="PGB570/60/600/60" Weight="10300" Bolt="36"/>
  <Profile Name="UC" Weight="8900" Bolt="30"/>
</Profiles>


And in my designer...
XAML:
XML
<Window x:Class="TEST_APP.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
        
        Title="TEST_APP" Height="280" Width="574""
        ResizeMode="CanMinimize">
    <Window.Resources>
        <XmlDataProvider x:Key="ProfileData" Source="Resources/ProfileList.xml" XPath="/Profiles/Profile"/>
    </Window.Resources>
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <TabControl HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch">
            <TabItem Header="Diagrid Diagonals">
                <Grid>
                    <DataGrid x:Name="profileData" HorizontalAlignment="Center" Height="147" Margin="0,0,0,20" VerticalAlignment="Center" Width="283" DataContext="{StaticResource ProfileData}" ItemsSource="{Binding XPath=/Profiles/Profile}" AutoGenerateColumns="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Profile" Binding="{Binding XPath= @Name}"/>
                            <DataGridTextColumn Header="Max. Weight" Binding="{Binding XPath=@Weight}"/>
                            <DataGridTextColumn Header="Bolt Dia" Binding="{Binding XPath=@Bolt}"/>
                        </DataGrid.Columns>
                    </DataGrid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="303"/>
                        <ColumnDefinition Width="153"/>
                        <ColumnDefinition Width="91"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</Window>


Everything works good in designer, and I can see the datagrid is populated with data. But on runtime its not loading, just an empty datagrid.
Please have a look and help me.

What I have tried:

The code worked very well on runtime also, but something happened now its not showing anything. That's why I am wondering!!.
I have tried modifying the XAML in different ways but no hope.Please consider as I am a begginer in WPF and XAML.
Posted
Updated 30-Jun-16 4:27am
v2

1 solution

No real solution, sorry..
But:
I tried your code (copied to a new project) and it worked as expected. So I would say there is nothing wrong with your xaml or XML. But I think it's best to set the XML.file to build-action None and copy to output directory or do you really need it as resource? So I would verify the file properties are correct and what you want. Other from wrong file Setting I have no idea would could have gone wrong with that... maybe start over again (I just needed 1 Minute to copy your code and data-file to a new solution), to rule out some project settings you may have changed unintentionally...
 
Share this answer
 
Comments
Manu Prasad 1-Jul-16 7:20am    
Don't know the reason but it is working again, when I changed Source="Resources/ProfileList.xml" to Source="/Resources/ProfileList.xml". Jsut one slash infront of "Resource". After reverted the change still it is stay there as required :).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900