Steps to create
- Create a new project.
- Design Graphical User Interface
- Add xml file, define root element Child elements & content
- Create a class
- Load the xml file into MainPage.xaml (Backend Code)
- Deploy the project
Create a project(VS 2013)
To create a new project go to file-->new project-->select windows Apps-->Blank App.Xaml
Interface Design
Open mainpage.xaml from solution explorer and write the code below
- <Grid>
-
- <TextBlock x:Name="tb1" HorizontalAlignment="Left" Margin="50,54,0,0" TextWrapping="Wrap" Text="My App" VerticalAlignment="Top" Height="55" Width="609" FontSize="36"/>
- <Grid Background="Transparent" HorizontalAlignment="Left" Margin="50,162,0,0" VerticalAlignment="Top" Width="274" Height="565">
- <ScrollViewer Height="565" Width="274" HorizontalScrollBarVisibility="Disabled" HorizontalScrollMode="Disabled" VerticalScrollBarVisibility="Auto" VerticalScrollMode="Auto">
- <StackPanel Height="2000" Width="274">
- <Button Content="Button" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="62" Click="Button_Click" Background="Green"/>
- <Button Content="Button" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="62" Click="b1_Click" Background="Purple"/>
- <Button Content="Button" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="59" Background="BlueViolet" Click="Button_Click_1"/>
- <Button Content="Button" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="59" Background="Orange" Click="Button_Click_2"/>
- <Button Content="Button" Background="BlueViolet" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="62" Click="Button_Click_3"/>
- </StackPanel>
- </ScrollViewer>
- </Grid>
- <ListView x:Name="l1" Height="680" Width="900" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="429,77,0,0">
- <ListView.ItemTemplate>
- <DataTemplate>
- <Grid Background="{Binding Color}">
- <TextBlock Text="{Binding Con}" TextWrapping="Wrap" TextAlignment="Justify" FontFamily="Segoe UI Light" FontSize="20" Margin="10,0,10,0" Width="860" Height="auto"/>
- </Grid>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- </Grid>
Adding xml file
To add an xml file follow the procedure:
Right Click On project name-->Click On Add-->New Item-->xml file-->Click Ok
Now add the folloing code in xml file.
- <items>
- <item1>
- <Con>
- This Is Item 1
- </Con>
- <Color>
- Green
- </Color>
- </item1>
- <item2>
- <Con>
- This Is Item 2
- </Con>
- <Color>
- Purple
- </Color>
- </item2>
- <item3>
- <Con>
- This Is Item 3
- </Con>
- <Color>
- BlueViolet
- </Color>
- </item3>
- <item4>
- <Con>
- This Is Item 4
- </Con>
- <Color>
- Orange
- </Color>
- </item4>
- <item5>
- <Con>
- This is an Additional item
- </Con>
- <Color>
- BlueViolet
- </Color>
- </item5>
- </items>
Creating a class
To add a class again:
Right Click On Project Name-->Add-->New Class -->click ok
Add the following code in Class:
- public class Class1
- {
- public string Con { get; set; }
- public string Color { get; set; }
- public Class1(string c,string col)
- {
- Con = c;
- Color = col;
- }
All is done now we need to load the data of an xml file into the grid view to do this add the following code to mainpage.xaml.cs
Before that you need to include these references
- using System.Xml.Linq;
- using System.Xml;
Now add the following code:
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- XDocument Xdoc = XDocument.Load("XMLFile1.xml");
- IEnumerable < Class1 > items = from item1 in Xdoc.Descendants("item1")
- select new Class1(
- item1.Element("Con").Value,
- item1.Element("Color").Value);
- l1.ItemsSource = items;
-
- }
-
- private void b1_Click(object sender, RoutedEventArgs e)
- {
-
- XDocument Xdoc = XDocument.Load("XMLFile1.xml");
- IEnumerable < Class1 > items = from item2 in Xdoc.Descendants("item2")
- select new Class1(
- item2.Element("Con").Value,
- item2.Element("Color").Value);
- l1.ItemsSource = items;
-
- }
-
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- XDocument Xdoc = XDocument.Load("XMLFile1.xml");
- IEnumerable < Class1 > items = from item3 in Xdoc.Descendants("item3")
- select new Class1(
- item3.Element("Con").Value,
- item3.Element("Color").Value);
- l1.ItemsSource = items;
- }
-
- private void Button_Click_2(object sender, RoutedEventArgs e)
- {
- XDocument Xdoc = XDocument.Load("XMLFile1.xml");
- IEnumerable < Class1 > items = from item4 in Xdoc.Descendants("item4")
- select new Class1(
- item4.Element("Con").Value,
- item4.Element("Color").Value);
- l1.ItemsSource = items;
- }
-
- private void Button_Click_3(object sender, RoutedEventArgs e)
- {
- XDocument Xdoc = XDocument.Load("XMLFile1.xml");
- IEnumerable < Class1 > items = from item5 in Xdoc.Descendants("item5")
- select new Class1(
- item5.Element("Con").Value,
- item5.Element("Color").Value);
- l1.ItemsSource = items;
- }
All is Done now run your App And check it!!