简体   繁体   English

数据绑定到WP7中的TextBlock

[英]Databinding to TextBlock in WP7

This is my XML file: 这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
    <Kids> 
      <Child> 
        <Name>Kid1</Name> 
        <FirstName>hisname</FirstName> 
      </Child> 
    <Child> 
        <Name>kid2</Name> 
        <FirstName>SomeName</FirstName> 
      </Child> 
    </Kids> 

I have used Linq to XML to read my xml file. 我已经使用Linq到XML来读取我的xml文件。 Now I want to databind the result to a textblock in my windows phone 7 application. 现在,我想将结果数据绑定到Windows Phone 7应用程序中的文本块。 I have a class called SerializeKidToXml. 我有一个名为SerializeKidToXml的类。 In that class I have a function called ReadXML that looks like this: 在该类中,我有一个名为ReadXML的函数,如下所示:

  public string ReadXml()
        {
            StringBuilder s = new StringBuilder();
            using (IsolatedStorageFileStream test = new IsolatedStorageFileStream("YourKids.xml", FileMode.Open, store))
            {

                var testxdoc = XDocument.Load(test);
                var returnval = from item in testxdoc.Descendants("Kids").Elements("Child")
                                select new
                                {
                                    kind = item.Element("FirstName").Value
                                };
                return s.Append(returnval).ToString();

            }
        }

Now I want the result of this query to bind with a textblock that is located on the interface of an XAML page. 现在,我希望此查询的结果与XAML页面界面上的文本块绑定。 I am trying to bind it by using code behind the XAML page. 我试图通过使用XAML页面后面的代码来绑定它。 This is what I have now: 这就是我现在所拥有的:

 private void button1_Click(object sender, RoutedEventArgs e)
        {
            SerializeKidToXml t = new SerializeKidToXml();
            textBlock1.Text = t.ReadXml();

        }

The textblock on the XAML page is not showing me the result string but instead this: System.LINQ.Enumerable...... XAML页面上的文本块未向我显示结果字符串,而是向我显示:System.LINQ.Enumerable ......

Any help would be appreciated. 任何帮助,将不胜感激。 Thx. 谢谢。

This might help you: 这可能对您有帮助:

  var returnval = from item in testxdoc.Descendants("Kids").Elements("Child")
         select item.Element("FirstName").Value;

     foreach(var str in returnval)
     { 
        s.Append(", ");
        s.Append(str);
     }
     return s.ToString();

If you want to show kids in a listbox: 如果要在列表框中显示孩子:

public IEnumerable<string> ReadXml()
        {
            using (IsolatedStorageFileStream test = new      IsolatedStorageFileStream("YourKids.xml", FileMode.Open, store))
            {    
                var testxdoc = XDocument.Load(test);
                var collection = from item in testxdoc.Descendants("Kids").Elements("Child")
                                select item.Element("FirstName").Value;
                return collection;

            }
        }

and use it: 并使用它:

   SerializeKidToXml t = new SerializeKidToXml();
   listBox.ItemsSource = t.ReadXml();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM