1

我想从隔离存储中的 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 = 索引;

还是不行。

4

1 回答 1

0

您必须调用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; }
    }
}
于 2013-01-27T20:18:16.033 回答