如何从silverlight中的observablecollection中删除自定义项目

时间:2012-11-12 06:04:47

标签: silverlight mvvm

我将一个可观察的cllection绑定到silverlight中的一个列表框。当我单击列表框中的一个项目并单击删除按钮时,如何删除该特定项目从列表框中删除没有linq使用mvvm.i传递按钮的命令参数是listbox itemid

 <ListBox   ItemsSource="{Binding School1,Mode=TwoWay}" DisplayMemberPath="SchoolName"  Name="listBox1"  >
<Button Content="Delete" Command="{Binding deletecommand}" CommandParameter="{Binding Path=SelectedItem.ID,ElementName=listBox1}"   Name="button2" />

那么从observable collection中删除特定项目的代码是什么

public void delete(object parameter)
{
School1.Remove(...)
}

1 个答案:

答案 0 :(得分:0)

将ListBox的SelectedItem绑定到属性并在Remove()中使用它:

 <ListBox ItemsSource="{Binding School1, Mode=TwoWay}" 
          DisplayMemberPath="SchoolName"  
          SelectedItem={Binding SelectedSchool}
          Name="listBox1"  
          />


public void delete(object parameter)
{
    if (SelectedSchool != null)
        School1.Remove(SelectedSchool);
}

另请注意,您的问题有点重复:Clearing selecteditem of listbox (which is bound to collection of objects) with MVVM

相关问题