2014-01-29 2 views
2

내 질문은 매우 간단하다고 생각했지만 해결책을 찾지 못했습니다.
각 항목에 LongListSelectorContextMenu이 있습니다. LongListSelector의 항목을 길게 누르면 ContextMenu에 삭제 옵션이 표시됩니다. 선택한 LongListSelector 항목을 삭제하고 싶습니다. 내 코드 :WP8 : ContextMenu를 사용하여 항목을 삭제하는 LongListSelector

XAML :

<phone:PhoneApplicationPage 
.... 
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
> 

<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <Grid x:Name="ContentPanel" Grid.Row="0" Margin="12,0,12,0"> 

     <phone:LongListSelector 
      Name="TestList" 
      > 
      <phone:LongListSelector.ItemTemplate 
       > 
       <DataTemplate> 
        <TextBlock Text="{Binding}"> 
        <toolkit:ContextMenuService.ContextMenu> 
         <toolkit:ContextMenu Name="ContextMenu" > 
          <toolkit:MenuItem 
           Name="Delete" 
           Header="Delete" 
           Click="Delete_Click"/> 
         </toolkit:ContextMenu> 
        </toolkit:ContextMenuService.ContextMenu> 
        </TextBlock> 
       </DataTemplate> 
       </phone:LongListSelector.ItemTemplate> 
     </phone:LongListSelector> 

    </Grid> 

</Grid> 

C 번호 :

namespace TestContextMenu 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     public List<string> Items = new List<string> 
     { 
      "Item1", 
      "Item2", 
      "Item3", 
      "Item4", 
      "Item5", 
     }; 

     public MainPage() 
     { 
      InitializeComponent(); 
      TestList.ItemsSource = Items; 
     } 


     private void Delete_Click(object sender, RoutedEventArgs e) 
     { 
      Items.RemoveAt(0); 
      //var item = (sender as MenuItem).DataContext; 
      //TestList.ItemsSource.Remove(item); 
     } 
    } 
} 

데이터를 가지고 있지만 난의 LongListSelector의 항목을 시각적으로 삭제할 수 없습니다, 삭제를 클릭

제거되었습니다.

나는 this을 읽었지만 내 상황에서는 해결책이 효과가 없습니다. 누구나 내 코드 pls에 뭐가 잘못 알고, 내게 알려 줘요, 고마워요!

답변

6

List<string>ObservableCollection<string>으로 바꾸십시오. ObservableCollection은 컬렉션의 변경 내용에 반응하도록 설계 되었기 때문에

+0

빠른 응답을 보내 주신 @ crea7or 감사합니다. 잘 작동합니다! – Vigor

관련 문제