如何将XAML文件中Window.Resources中包含的XML数据连接到XML.Linq

时间:2016-10-04 21:59:57

标签: c# xml wpf linq xaml

我已将表示NATO语音字母表的XML数据放入XAML窗口文件中。我还有代码,我想以编程方式生成每个条目的Stack Panels,每个包含2个标签,一个包含Letter,第二个是表示它的Phonetic单词。它将全部放在一个包装面板中。我可以做的很少只是手动制作xaml,但这是练习,我真的想要签出Linq和XML。后面的代码包含所有注释。这是我第一次在代码中使用Linq和XML,所以我不知道自己在做什么,所以请解释概念并添加链接而不仅仅是答案。

这是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>

这是背后的代码:

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;
    }

}

1 个答案:

答案 0 :(得分:1)

看一下这个链接,应该很容易让代码适应你的XAML XML:

https://www.intertech.com/Blog/query-an-xml-document-using-linq-to-xml/