我对Windows Phone的编程很新,我在ListBox中编辑数据时遇到了麻烦 我正在将XML文件中的数据加载到列表中,并将它们显示在ListBox中 我的XML中的每个条目都附加了一个类型。我想通过选中/取消选中某些CheckBox来删除/添加某些类型的条目。
我发现了这样的事情:
How to remove all ListBox items?
但它并不完全符合我的需要,因为我只想删除部分ListBox项目,然后只需再次勾选复选框就可以取回它。
此外,我不想手动添加每个条目,因为我已经将listbox与包含我的XML条目的列表绑定在一起。
我像这样上传我的XML:
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"));
}
}
}
这是我的页面代码:
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)
{
}
ListBox的xaml:
<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>
此处还有我的XML文件
<?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>
你能帮帮我吗?我很困难,可能会遗漏一些明显的东西。提前谢谢。
答案 0 :(得分:0)
你应该这样做:
创建过滤器列表
选中/取消选中复选框时更新过滤器列表,并在每次过滤器更改时更新列表
private List<Activity> activities;
public void ActivityDisplay()
{
activities = Activity.LoadActivities();
FilterActivities();
}
public ObservableCollection<string> ActivityFilter = new ObservableCollection<string>();
private void FilterActivities()
{
listbox1.ItemsSource = null; //sometimes it makes the difference;
listBox1.ItemsSource = activities.Where(activity => ActivityFilter.Any(filter => filter == activity.Type) || ActivityFilter.Count == 0);
}
public PivotPage1()
{
InitializeComponent();
ActivityFilter.CollectionChanged += ActivityFilter_CollectionChanged;
ActivityDisplay();
}
void ActivityFilter_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
FilterActivities();
}
private void CookingCheck_Checked(object sender, RoutedEventArgs e)
{
ActivityFilter.Add("Cooking");
}
private void CookingCheck_Unchecked(object sender, RoutedEventArgs e)
{
ActivityFilter.Remove("Cooking");
}
//Repeat the procedure for each checkbox
我在ItemTemplate中发现了一个使用此问题的问题
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10">
<TextBlock Text="{Binding Description}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>