2013-08-30 63 views
2

我有一個綁定列表框,當我添加項目它的作品完美,但如果我嘗試刪除與contextMenu的項目它不工作。ObservableCollection ListBox項刪除不工作

這是我嘗試到目前爲止列表框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; 

它工作完美!現在我想刪除一個選定的項目與這樣的ContextMenu

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

    personList.RemoveAt(indexPerson); 
}  

但它不工作。任何人都有一個想法我做錯了什麼? 感謝

玉傢伙我現在有解決方案問題是SelectedIndex的價值,現在香港專業教育學院得到了正確的價值。首先IVE把文本菜單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); }; 
    }  
+0

不,如果你添加的項目它的工作(s)在將'personList'分配給'ItemsSource'之前?如果不是,那就是一個有約束力的問題。此外,是一個錯字 - 應該是「ItemsSource」而不是「ItemSource」。 – RobSiklos

+0

不以什麼方式工作?例外,或沒有例外和項目仍在列表中? – weston

+0

感謝球員的答案。我已調試它,並有問題機智listBox.SelectedIndex值是-1。所以ContextMenu是不是在正確的地方,因爲我沒有得到選定的索引 – iNCEPTION

回答

0

嘗試添加該表單時初始化:

lstPersons.ItemsSource = personList ; 

How to: Create and Bind to an ObservableCollection

看來,這個問題是由於選定項目

關鍵是在 正確的位置設置PreviewMouseRightButtonDown事件。正如你會注意到的,即使沒有ContextMenu權利 點擊一個ListViewItem將選擇該項目,所以我們需要 在每個項目上設置事件,而不是在ListView上。

<ListView> 
    <ListView.ItemContainerStyle> 
     <Style TargetType="{x:Type ListViewItem}"> 
      <EventSetter Event="PreviewMouseRightButtonDown" 
         Handler="OnListViewItemPreviewMouseRightButtonDown" /> 
     </Style> 
    </ListView.ItemContainerStyle> 
    <ListView.ContextMenu> 
     <ContextMenu> 
      <MenuItem Header="Menu Item">Item 1</MenuItem> 
      <MenuItem Header="Menu Item">Item 2</MenuItem> 
     </ContextMenu> 
    </ListView.ContextMenu> 
    <ListViewItem>Item</ListViewItem> 
    <ListViewItem>Item</ListViewItem> 
    <ListViewItem>Item</ListViewItem> 
    <ListViewItem>Item</ListViewItem> 
    <ListViewItem>Item</ListViewItem> 
    <ListViewItem>Item</ListViewItem> 
</ListView> 

private void OnListViewItemPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    Trace.WriteLine("Preview MouseRightButtonDown"); 

    e.Handled = true; 
} 

wpf listview right-click problem

+0

感謝您的答案。我試過了,但我認爲刪除一個項目的ContextMenu刪除事件代碼不起作用。 – iNCEPTION

+2

你確定'lstPerson.SelectedIndex'具有正確的值嗎? –

+0

問題是SelectedIndex的值也許上下文菜單不是在正確的地方或我有假事件 – iNCEPTION

0

除了什麼瓊說,你也可以做到這一點在XAML這樣的:

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

</ListBox> 

你應該閱讀如何使用綁定和MVVM的模型。這樣的東西非常方便。 這裏有一些鏈接: http://channel9.msdn.com/Events/MIX/MIX10/EX14 http://channel9.msdn.com/Events/MIX/MIX11/OPN03

不要氣餒。這在開始時是有點學習的,但它是完全值得的。在我使用Laurent Bugnions的MvvmLight包開始使用MVVM做所有事情之前,我也遇到了顯示列表的一些問題。

0

試試這個:

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 
        } 
     } 


    } 
+0

感謝您的答覆。我已調試它,並有問題機智listBox.SelectedIndex值是-1。所以ContextMenu不在正確的位置,因爲我沒有得到所選的索引 – iNCEPTION

0

爲什麼不使用所有具有約束力。

綁定項目源作爲的ObservableCollection布拉布拉

綁定的SelectedItem爲XXXX

爲您的按鈕使用的命令,當你想刪除的項目做:

blabla.remove(SelectedItem);