【问题标题】:Reading XML data to StackPanel controls将 XML 数据读取到 StackPanel 控件
【发布时间】:2013-01-27 19:36:41
【问题描述】:

我想从隔离存储中的 XML 文件加载数据。然后我有可变的特定索引号,即 XML 元素索引。在这里我有一个问题,因为我想从这个元素加载 TextBoxes(在普通 StackPanel 中)值。我尝试绑定并将其放入列表框中,但我无法从该框中读取文本,因为它是列表框项目。只是我想将元素属性加载到文本框,然后我想在这个文本框中阅读编辑过的文本。 这是示例 xml 元素:

<person index="1" att1="qwerty" att2="azerty" att3="abcdef"/>

这是 Xaml 代码:

<StackPanel x:Name="stack">
 <TextBlock Height="27" Margin="0,0,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Record index:" VerticalAlignment="Top" Foreground="#FF6C6C6C"/>
 <TextBox Text="{Binding Index}" x:Name="index_box_det" Height="65" Margin="-12,-10,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Foreground="#FF40AA2F" HorizontalAlignment="Left" Width="467" SelectionBackground="#FF40AA2F" SelectionForeground="White" BorderBrush="#FF3FA92E" FontSize="18.667"/>
</StackPanel>

我试过这个:

var ind = "1";
        using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("People2.xml", FileMode.Open, isoStore))
            {
                XDocument loadedCustomData = XDocument.Load(isoStream);
                var filteredData = from c in loadedCustomData.Descendants("person")
                                   where c.Attribute("index").Value == ind
                                   select new Person()
                                   {
                                       index= c.Attribute("index").Value,
                                       att1= c.Attribute("att1").Value,
                                       att2= c.Attribute("att2").Value,
                                       att3= c.Attribute("att3").Value
                                   };
                stack.DataContext = filteredData;
            }

但正如你所想,它不起作用。有人知道将此值加载到文本框吗?

编辑: 我试过这个:

 var ind = "1";
        using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("People2.xml", FileMode.Open, isoStore))
            {
                XDocument loadedCustomData = XDocument.Load(isoStream);
                var filteredData = from c in loadedCustomData.Descendants("person")
                                   where c.Attribute("index").Value == ind
                                   select new Person()
                                   {
                                       index= c.Attribute("index").Value,
                                       att1= c.Attribute("att1").Value,
                                       att2= c.Attribute("att2").Value,
                                       att3= c.Attribute("att3").Value
                                   };
                stack.DataContext = filteredData;
            }

index_box_det.Text = 索引;

还是不行。

【问题讨论】:

    标签: c# xml windows-phone-7 binding isolatedstorage


    【解决方案1】:

    你必须调用FirstOrDefault方法:

    var ind = "1";
    using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("People2.xml", FileMode.Open, isoStore))
        {
            XDocument loadedCustomData = XDocument.Load(isoStream);
            var filteredData = (from c in loadedCustomData.Descendants("person")
               where c.Attribute("index").Value == ind
               select new Person()
               {
                   Index = c.Attribute("index").Value,
                   Att1 = c.Attribute("att1").Value,
                   Att2 = c.Attribute("att2").Value,
                   Att3 = c.Attribute("att3").Value
               }).FirstOrDefault();
            stack.DataContext = filteredData;
        }
    }
    

    Person类:

    public class Person
    {
        string index;
        string att1;
        string att2;
        string att3;
    
        public string Index
        {
            get { return index; }
            set { index = value; }
        }
    
        public string Att1
        {
            get { return att1; }
            set { att1 = value; }
        }
    
        public string Att2
        {
            get { return att2; }
            set { att2 = value; }
        }
    
        public string Att3
        {
            get { return att3; }
            set { att3 = value; }
        }
    }
    

    【讨论】:

    • 对不起,我改了,但还是不行。编译器没有显示任何错误,但仍然没有显示任何值。
    • @ŁukaszWróblewski 你做了什么改变?
    • == 1 到 == "1"。 Tak naprawdę jak kopiowałem to przez nieuwage skasowałem nawiasy ;p
    • @ŁukaszWróblewski 不要忘记在绑定中将“索引”更改为“索引”(第一个错误)。
    • @ŁukaszWróblewski 再次检查我的答案。
    猜你喜欢
    • 1970-01-01
    • 2013-07-23
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多