Reputation: 337
I have put XML data which represents the NATO phonetic alphabet into XAML window File. I have also code behind which I want to generate programatically Stack Panels per Entry which will contain 2 labels each, One containing the Letter and second the Phonetic word representing it. It will all be put in a wrap panel. I could have done a lot less just making the xaml manually but this is practice, and I really want to checkout Linq and XML. The code behind contains all the comments. This is my first time using Linq and XML in code so I don't really know what I am doing so Please explain the concepts and add links and not only answers.
Here is the XAML:
<!--The XML database of letter of the phonetic alphabet this will be moved to seprate file-->
<Window.Resources>
<XmlDataProvider x:Key="PhoneticAlphabet" XPath="Alphabet/Entries" >
<x:XData>
<Alphabet xmlns="">
<Entries>
<Entry>
<Letter>Aa</Letter>
<Phonetic>Alpha</Phonetic>
</Entry>
<Entry>
<Letter>Bb</Letter>
<Phonetic>Bravo</Phonetic>
</Entry>
<Entry>
<Letter>Cc</Letter>
<Phonetic>Charlie</Phonetic>
</Entry>
<Entry>
<Letter>Dd</Letter>
<Phonetic>Delta</Phonetic>
</Entry>
<Entry>
<Letter>Ee</Letter>
<Phonetic>Echo</Phonetic>
</Entry>
<Entry>
<Letter>Ff</Letter>
<Phonetic>Foxtrot</Phonetic>
</Entry>
<Entry>
<Letter>Gg</Letter>
<Phonetic>Golf</Phonetic>
</Entry>
<Entry>
<Letter>Hh</Letter>
<Phonetic>Hotel</Phonetic>
</Entry>
<Entry>
<Letter>Ii</Letter>
<Phonetic>India</Phonetic>
</Entry>
<Entry>
<Letter>Jj</Letter>
<Phonetic>Juliett</Phonetic>
</Entry>
<Entry>
<Letter>Kk</Letter>
<Phonetic>Kilo</Phonetic>
</Entry>
<Entry>
<Letter>Ll</Letter>
<Phonetic>Lima</Phonetic>
</Entry>
<Entry>
<Letter>Mm</Letter>
<Phonetic>Mike</Phonetic>
</Entry>
<Entry>
<Letter>Nn</Letter>
<Phonetic>November</Phonetic>
</Entry>
<Entry>
<Letter>Oo</Letter>
<Phonetic>Oscar</Phonetic>
</Entry>
<Entry>
<Letter>Pp</Letter>
<Phonetic>Papa</Phonetic>
</Entry>
<Entry>
<Letter>Qq</Letter>
<Phonetic>Quebec</Phonetic>
</Entry>
<Entry>
<Letter>Rr</Letter>
<Phonetic>Romeo</Phonetic>
</Entry>
<Entry>
<Letter>Ss</Letter>
<Phonetic>Sierra</Phonetic>
</Entry>
<Entry>
<Letter>Tt</Letter>
<Phonetic>Tango</Phonetic>
</Entry>
<Entry>
<Letter>Uu</Letter>
<Phonetic>Uniform</Phonetic>
</Entry>
<Entry>
<Letter>Vv</Letter>
<Phonetic>Victor</Phonetic>
</Entry>
<Entry>
<Letter>Ww</Letter>
<Phonetic>Whiskey</Phonetic>
</Entry>
<Entry>
<Letter>Xx</Letter>
<Phonetic>Xray</Phonetic>
</Entry>
<Entry>
<Letter>Yy</Letter>
<Phonetic>Yankee</Phonetic>
</Entry>
<Entry>
<Letter>Zz</Letter>
<Phonetic>Zulu</Phonetic>
</Entry>
</Entries>
</Alphabet>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Window.DataContext>
<Binding Source="{StaticResource PhoneticAlphabet}"
XPath="*"/>
</Window.DataContext>
<Grid>
<WrapPanel x:Name="MainPanel">
</WrapPanel>
</Grid>
Here is the code behind:
public partial class PhoneticAlphabetWindow : Window
{
public PhoneticAlphabetWindow()
{
InitializeComponent();
}
private void setControls() {
//Get XML data
//For ech xml Entry
//Make a stack Panel
//Make two labels with letter and phonetic and add them to stack panel
//Add a style to label
//add a content to be letter
//make a label with phonetic word
//addd a style to label
//add a phonetic
}
private IEnumerable<XElement> getData(String xml) {
XDocument doc = XDocument.Load(xml);
IEnumerable<XElement> childList =
from el in doc.Elements()
select el;
return childList;
}
private void addLabels(StackPanel EntryPanel, String letter, String phonetic) {
Label letterLabel = new Label();
letterLabel.Content = letter;
Label phoneticLabel = new Label();
phoneticLabel.Content = phonetic;
EntryPanel.Children.Add(letterLabel);
EntryPanel.Children.Add(phoneticLabel);
}
private StackPanel addStackPanel() {
StackPanel EntryPanel = new StackPanel();
EntryPanel.Orientation = Orientation.Horizontal;
MainPanel.Children.Add(EntryPanel);
return EntryPanel;
}
}
Upvotes: 0
Views: 225
Reputation: 85
Have a look to this link, it should be easy to adapt the code to you XAML XML:
https://www.intertech.com/Blog/query-an-xml-document-using-linq-to-xml/
Upvotes: 1