2011-07-19 95 views
1

我想在自定义列表中显示之前提取的数据,但是我发现提取该数据的方法对我来说并不容易,所以我会找到一种方法来显示我的数据而不更改xml读取方法。 这是我多么希望我的名单为(XML):自定义列表框WP7

<ListBox Height="516" HorizontalAlignment="Left" Margin="16,74,0,0" Name="listBox1" VerticalAlignment="Top" Width="430" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" Height="132"> 
         <Image Source="{Binding wkpinImage}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/> 
         <StackPanel Width="370"> 
          <TextBlock Text="{Binding Day}" Foreground="#FFC8AB14" FontSize="28" /> 
          <TextBlock Text="{Binding Low}" TextWrapping="Wrap" FontSize="24" /> 
          <TextBlock Text="{Binding High}" TextWrapping="Wrap" FontSize="24" /> 
          <TextBlock Text="{Binding Condition}" TextWrapping="Wrap" FontSize="26" /> 
         </StackPanel> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

这是XML阅读方法(C#):

while (reader.Read()) 
      { 
       switch (reader.Name) 
       { 
        case ("day_of_week"): 
         { 
          listBox1.Items.Add(new ListBoxItem() 
           { 
            Content = reader.GetAttribute("data") 
           }); 
          Day = Content.ToString(); 
         } break; 

...

回答

0

我会建议使用MVVM方法将项目绑定到您的列表框。

下面是如何MVVM项绑定到一个列表框一个很好的教程:

http://www.labo-dotnet.com/post/Creating-your-first-MVVM-silverlight-application-on-windows-phone-7.aspx

有好点的大量的有关MVVM:

  • 少编码
  • 您的绑定可针对庞大数据源进行优化
  • 更易于维护
  • 更干净的代码
  • ...
+0

我想要的东西更容易,是不是有一个? – MarTech

+0

以我的观点否:)放松一下,它非常健壮。只要按照教程,Channel9上也有很多视频 –

0

在我看来,最好的解决方案在所有的XML序列化!

您应该简单地创建一个序列化类 ( REMEMEBER: - 使用基本数据类型(否则你将需要翻译的属性) - 只有公共属性可以序列 - 保持一个构造函数不带参数 )

... 
using System.Xml.Serialization; 

public class SerializableClass 
{ 
    [XmlAttribute(AttributeName = "Day")] 
    public int Day 
    { 
     get 
     { 
      ... 
     } 
     set 
     { 
      ... 
     } 
    } 

    [XmlIgnore] 
    public CustomEnumerationType PublicPropertyNotToReadWrite 
    { 
     get 
     { 
      ... 
     } 
     set 
     { 
      ... 
     } 
    } 

    ... 
} 

一个比使用这些静态方法序列化(输出你的类的对象到SOMETHING [例如XML文件/流])和反序列化(INPUT FROM SOMETHING [例如XML文件/流])的新实例你的班级的一个对象):

(THE NEXT 2静态方法可以读/写Serializable对象FROM/TO AN ISOLATEDSTORAGE FILE !!)

公共静态对象DeserializeObject(字符串文件名,类型的objectType) { 使用(IsolatedStorageFile appStorage = IsolatedStorageFile。 GetUserStoreForApplication()) 使用(IsolatedStorageFileStream FILESTREAM = appStorage.OpenFile(文件名,FileMode.Open,FileAccess.Read)) 使用(的TextReader的XmlReader =新的StreamReader(FILESTREAM)) { XmlSerializer的XmlSerializer的新= XmlSerializer的(的objectType);

  return xmlSerializer.Deserialize(xmlReader); 
     } 
    } 

    public static void SerializeObject(string fileName, object target, Type objectType) 
    { 
     using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
     using (IsolatedStorageFileStream fileStream = appStorage.OpenFile(fileName, FileMode.Create, FileAccess.Write)) 
     using (TextWriter xmlWriter = new StreamWriter(fileStream)) 
     { 
      XmlSerializer xmlSerializer = new XmlSerializer(objectType); 

      xmlSerializer.Serialize(xmlWriter, target); 
     } 
    } 

没有什么比这种方法更好,在我看来!

我希望这能帮助你!