我有ListView
一个用户可以从choreList
. 但是,我希望选择的项目保存到另一个模型,其中包含Chores
下面是我的尝试,但它不起作用。该personSingle
对象包含 0assignedChores
<ListView x:Name="choreList" BorderBrush="White" BorderThickness="1" Grid.Row="1"
DisplayMemberPath="Summary" ItemsSource="{Binding choreList, Mode=OneWay}"
SelectedValue="{Binding personSingle.assignedChores, Mode=TwoWay}"
SelectionMode="Multiple"/>
我需要做什么才能将选择的家务添加到我的personSingle.assignedChores
列表中?
更新答案:
private void choreList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count > 0) {
foreach(win8Chores.Model.databaseTables.Chore item in e.RemovedItems)
{
win8Chores.Model.databaseTables.Chore myChore = new Model.databaseTables.Chore();
myChore = item;
VM.personSingle.assignedChores.Remove(myChore);
}
}
if (e.AddedItems.Count > 0)
{
foreach (win8Chores.Model.databaseTables.Chore item in e.AddedItems)
{
win8Chores.Model.databaseTables.Chore myChore = new Model.databaseTables.Chore();
myChore = item;
VM.personSingle.assignedChores.Add(myChore);
}
}
}
XAML:
<ListView x:Name="choreList" BorderBrush="White" BorderThickness="1" Margin="401,322,613,150" Grid.Row="1"
DisplayMemberPath="Summary" ItemsSource="{Binding choreList, Mode=OneWay}"
SelectionMode="Multiple" SelectionChanged="choreList_SelectionChanged"/>
我仍然想知道是否有办法严格使用绑定来做到这一点。但是现在 SelectionChanged 事件起作用了。