I'm pretty new to programming for Windows Phone and I'm having trouble editing data in ListBox.
I'm loading data from my XML file into a list, and displaying them in ListBox.
Each entry in my XML has a type attached to it. I want to make it so I can remove/add entries with certain types, by checking/unchecking certain CheckBoxes.
I found something like this:
How to remove all ListBox items?
But it doesn't exactly suit my needs since I want only to delete part of ListBox items, and then possibly get it back by simply checking the checkbox again.
Also I don't want to add every entry manually since I already bound listbox with a list containing my XML entries.
I upload my XML like so:
Activity.cs
:
namespace ReadXMLfromFile
{
public class Activity
{
public string Type { get; private set; }
public string Description { get; private set; }
public Activity(string type, string description)
{
Type = type;
Description = description;
}
public static List<Activity> LoadActivities(XDocument doc)
{
return doc .Root
.Elements("Activity")
.Select(el => new Activity((string)el.Attribute("Type"),
el.Value))
.ToList();
}
public static List<Activity> LoadActivities()
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists("Activities.xml"))
{
using (var stream = store.OpenFile("Activities.xml",
FileMode.Open))
{
return LoadActivities(XDocument.Load(stream));
}
}
}
// No user file... load the default activities
return LoadActivities(XDocument.Load("Assets/Activities.xml"));
}
}
}
Here is my code for my page:
namespace ActivityManager
{
public partial class PivotPage1 : PhoneApplicationPage
{
private List<Activity> activities;
public void ActivityDisplay()
{
activities = Activity.LoadActivities();
listBox1.ItemsSource = activities;
}
public PivotPage1()
{
InitializeComponent();
ActivityDisplay();
}
private void CookingCheck_Checked(object sender, RoutedEventArgs e)
{
}
private void ArtCheck_Checked(object sender, RoutedEventArgs e)
{
}
private void LanguageCheck_Checked(object sender, RoutedEventArgs e)
{
}
private void ScienceCheck_Checked(object sender, RoutedEventArgs e)
{
}
private void SportCheck_Checked(object sender, RoutedEventArgs e)
{
}
And xaml for ListBox:
<ListBox Height="347" HorizontalAlignment="Left" Margin="27,243,0,0"
Name="listBox1" VerticalAlignment="Top" Width="410"
SelectionChanged="listBox1_SelectionChanged" AllowDrop="False"
ItemsSource="{Binding activities}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10">
<TextBlock Text="{Binding Activity}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Also here is my XML file
<?xml version="1.0" encoding="utf-8" ?>
<Activities>
<Activity Type="Cooking">
Cook some Mexican food
</Activity>
<Activity Type="Art">
Draw a picture
</Activity>
<Activity Type="Language">
Learn 5 words in Polish you didn't know yet
</Activity>
<Activity Type="Science">
Write a simple program in C
</Activity>
<Activity Type="Sport">
Go to the gym
</Activity>
</Activities>
Can you help me? I'm pretty stuck and probably missing something obvious. Thank you in advance.