Customizing ListBox WP7
I want to display the data extracted before in a customized list, but the method i found to ex开发者_JS百科tract that data doesn't make things easy for me , so i would find a way to display my data without changing the xml reading method. This is how i want my list to be(xml):
<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>
And this is the xml reading method(c#):
while (reader.Read())
{
switch (reader.Name)
{
case ("day_of_week"):
{
listBox1.Items.Add(new ListBoxItem()
{
Content = reader.GetAttribute("data")
});
Day = Content.ToString();
} break;
...
I would recommend using MVVM approach to bind items to your listbox.
Here is a very good tutorial of how to MVVM to bind items to a listbox:
http://www.labo-dotnet.com/post/Creating-your-first-MVVM-silverlight-application-on-windows-phone-7.aspx
There are lot's of good points about MVVM:
- Less coding
- Your binding can be optimized for huge datasources
- easier maintainance
- cleaner code
- ...
In my opinion, the best solution AT ALL is XML serialization!
You should simply create a serializable class ( REMEMEBER: - use base data types (otherwise you'll need translation properties) - only public properties can be serialized - keep a constructor WITHOUT parameters )
...
using System.Xml.Serialization;
public class SerializableClass
{
[XmlAttribute(AttributeName = "Day")]
public int Day
{
get
{
...
}
set
{
...
}
}
[XmlIgnore]
public CustomEnumerationType PublicPropertyNotToReadWrite
{
get
{
...
}
set
{
...
}
}
...
}
An than use these static methods to serialize (OUTPUT the object of your class TO SOMETHING [eg. XML file / stream]) and deserialize (INPUT FROM SOMETHING [eg. XML file / stream] TO a new instance of an object of your class):
(THE NEXT 2 STATIC METHODS CAN READ/WRITE A SERIALIZABLE OBJECT FROM/TO AN ISOLATEDSTORAGE FILE!!)
public static object DeserializeObject(string fileName, Type objectType) { using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication()) using (IsolatedStorageFileStream fileStream = appStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read)) using (TextReader xmlReader = new StreamReader(fileStream)) { XmlSerializer xmlSerializer = new 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);
}
}
There's nothing better than this approach, in my opinion!
I hope this can help you!
精彩评论