我已经创建了一个列表框,并通过将其绑定到我从XML文件创建的列表中来填充数据。我这样做了,每个条目都是按钮的形式。
现在我要传输"描述"按钮的内容(点击它时)到另一个列表框,添加了当前日期。有一个简单的方法吗?这是我的XAML:
<ListBox Height="395" HorizontalAlignment="Left" Margin="27,195,0,0" Name="listBox1" VerticalAlignment="Top" Width="410" AllowDrop="False" ItemsSource="{Binding activities}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Height="150" Width="400" Click="Act_Click">
<Grid>
<TextBlock Text="{Binding Type}" TextAlignment="Left"/>
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" TextAlignment="Left"/>
</Grid>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Height="491" HorizontalAlignment="Left" Margin="10,105,0,0" Name="listBox2" VerticalAlignment="Top" Width="436" ItemsSource="{Binding activities2}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding activities2}" TextWrapping="Wrap" TextAlignment="Left"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
以下是我从XML读取文件的方法:
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"));
}
}
}
答案 0 :(得分:0)
我建议将Listbox上的SelectedItem绑定到活动的实例,当该属性发生更改时检查是否已将与该项关联的描述添加到您的第二个列表中,如果没有,则添加它。
答案 1 :(得分:0)
简单:
var activities = (List<Activity>)listbox1.ItemsSource;
var myObject = (Activity)listbox1.SelectedItem;
activities.Remove(listbox1.SelectedItem);
myObject.Date = DateTime.Now; //to provide your last request(add the current date)
var activities2 = (List<Activity>)listbox2.ItemsSource;
if(activities2 == null)
{
activities2 = new List<Activity>();
listbox2.ItemsSource = activities2;
}
activities2.Add(myObject);
确保ListBox更新其项目
listbox1.ItemsSource = null;
listbox1.ItemsSource = activities;
和
listbox2.ItemsSource = null;
listbox2.ItemsSource = activities2;
或仅使用ObservableCollection<Activity>
代替List<Activity>
,ListBoxes应自动更新其项目。
编辑:修复第二个ListBox的ItemTemplate
<ListBox Height="395" HorizontalAlignment="Left" Margin="27,195,0,0" Name="listBox1" VerticalAlignment="Top" Width="410" AllowDrop="False" ItemsSource="{Binding activities}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Height="150" Width="400" Click="Act_Click">
<Grid>
<TextBlock Text="{Binding Type}" TextAlignment="Left"/>
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" TextAlignment="Left"/>
</Grid>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Height="491" HorizontalAlignment="Left" Margin="10,105,0,0" Name="listBox2" VerticalAlignment="Top" Width="436" ItemsSource="{Binding activities2}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Height="150" Width="400">
<Grid>
<TextBlock Text="{Binding Type}" TextAlignment="Left"/>
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" TextAlignment="Left"/>
</Grid>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>