Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ObservableCollection列表框项删除不工作_C#_Silverlight_Xaml_Windows Phone 8 - Fatal编程技术网

C# ObservableCollection列表框项删除不工作

C# ObservableCollection列表框项删除不工作,c#,silverlight,xaml,windows-phone-8,C#,Silverlight,Xaml,Windows Phone 8,我有一个带有绑定的列表框,当我添加项目时,它工作得很好,但是如果我尝试使用contextMenu删除项目,它就不工作了 以下是我到目前为止尝试的列表框Xaml代码 <ListBox Name="lstPersons" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="126,-228,2,-242">

我有一个带有绑定的列表框,当我添加项目时,它工作得很好,但是如果我尝试使用contextMenu删除项目,它就不工作了

以下是我到目前为止尝试的列表框Xaml代码

   <ListBox Name="lstPersons"
                     VerticalAlignment="Stretch"
                     HorizontalAlignment="Stretch" Margin="126,-228,2,-242">
                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu Name="PersonContext">
                        <toolkit:MenuItem Name="PersonDelete" Header="Delete" Click="DeletePerson_Click"/>
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>    
                            <TextBlock Name="btnKellnerName"                                      
                                             Text="{Binding _PersonName}" 
                                             FontSize="35" 
                                             FontFamily="Portable User Interface"/>
                            <TextBlock Name="btnPosition"
                                             Text="{Binding _PersonPosition}"
                                             FontSize="22"/>
                            <TextBlock Name="lblUhrzeit" 
                                             Text="{Binding _CreationDate}"
                                             FontSize="18"/>
                            <TextBlock Name="Space" Text="                "/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
public class Person 
{    
    public string _PersonName { get; set; }      
    public string _PersonPosition { get; set; }    
    public string _CreationDate { get; set; }   
}
当我添加这样的项目时

ObservableCollection<Person> personList = new ObservableCollection<Person>();

personList.Add(new Person { 
_PersonName = "Tom",
_PersonPosition = "Bla", 
_CreationDate = "33"
});

this.lstPerson.ItemSource = personList;
private void DeletePerson_Click(object sender, RoutedEventArgs e)
{   
   int indexPerson = lstPerson.SelectedIndex;

   personList.RemoveAt(indexPerson);
}    
但它不起作用。有人知道我做错了什么吗谢谢

   <ListBox Name="lstPersons"
                     VerticalAlignment="Stretch"
                     HorizontalAlignment="Stretch" Margin="126,-228,2,-242">
                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu Name="PersonContext">
                        <toolkit:MenuItem Name="PersonDelete" Header="Delete" Click="DeletePerson_Click"/>
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>    
                            <TextBlock Name="btnKellnerName"                                      
                                             Text="{Binding _PersonName}" 
                                             FontSize="35" 
                                             FontFamily="Portable User Interface"/>
                            <TextBlock Name="btnPosition"
                                             Text="{Binding _PersonPosition}"
                                             FontSize="22"/>
                            <TextBlock Name="lblUhrzeit" 
                                             Text="{Binding _CreationDate}"
                                             FontSize="18"/>
                            <TextBlock Name="Space" Text="                "/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
public class Person 
{    
    public string _PersonName { get; set; }      
    public string _PersonPosition { get; set; }    
    public string _CreationDate { get; set; }   
}
好了,伙计们,我现在有了解决方案,问题是SelectedIndex的值,现在我得到了正确的值。首先,我把ContextMenu放在ListBoxItemTemplate/StackPanel中

代码隐藏:

private void DeletePerson_Click(object sender, RoutedEventArgs e)
  {
      try {

                var selectedListBoxItem = listBox.ItemContainerGenerator.ContainerFromItem(((MenuItem) sender).DataContext) as ListBoxItem;
                var selectedIndex = listBox.ItemContainerGenerator.IndexFromContainer(selectedListBoxItem);

                _personList.RemoveAt(selectedIndex);
         }
         catch( Exception ex ) { MessageBox.Show(ex.Message); };
  }    

初始化表单时尝试添加以下内容:

lstPersons.ItemsSource = personList ;

问题似乎是由所选项目引起的

关键是在中设置PreviewMouseRightButtonDown事件 正确的位置。正如您所注意到的,即使没有上下文菜单,对吗 单击ListViewItem将选择该项目,因此我们需要 在每个项目上设置事件,而不是在ListView上


除了Joan所说的,您还可以在Xaml中这样做:

<ListBox ItemsSource={Binding personList}
                 VerticalAlignment="Stretch"
                 HorizontalAlignment="Stretch" Margin="126,-228,2,-242">
            <toolkit:ContextMenuService.ContextMenu>

</ListBox>

您应该阅读如何使用绑定和MVVM模型。这样的东西很方便。 以下是一些链接:

不要气馁。这是一开始的一点学习,但完全值得。在使用Laurent Bugnons的MvvmLight软件包开始使用MVVM之前,我在显示列表方面也遇到了一些问题。

试试以下方法:

private void DeletePerson_Click(object sender, RoutedEventArgs e)
    {   
       System.Collections.IList pathRemove;
       pathRemove = lstPerson.SelectedItems;

       if(pathRemove.Count != 0)
          {
               for (int i = pathRemove.Count - 1; i >= 0; i--)
                    {
                        lstPerson.Remove((Person)pathRemove[i]);//multiple deletes
                    } 
         }


    } 

为什么不为所有人使用绑定呢

将项目源绑定为ObservableCollection blabla

将Selecteditem绑定为XXXX

对按钮使用命令,并在需要删除项目时执行以下操作:

blabla.remove(SelectedItem);

谢谢你的回答。我试过了,但我认为用于删除项目的ContextMenu Delete event Code不起作用。你确定lsPerson.SelectedIndex的值正确吗?问题是SelectedIndex的值可能上下文菜单不在正确的位置,或者我有错误的事件谢谢老兄,但我最终解决了问题!太好了!:)祝您有个美好的一天!如果在将
personList
分配给
ItemsSource
之前添加项目,是否有效?如果不是,则存在绑定问题。另外,输入错误应该是
ItemSource
而不是
ItemSource
。以什么方式不起作用?例外,或者没有例外,项目仍在列表中?感谢各位的回答。我已经调试过了,listBox.SelectedIndex的值为-1时出现问题。因此,上下文菜单不在正确的位置,因为我没有为答案选择索引。我已经调试过了,listBox.SelectedIndex的值为-1时出现问题。所以ContextMenu不在正确的位置,因为我没有得到所选的索引