0

我試圖從不同的XAML中單擊按鈕時從ObservableCollection中刪除所有選定的項目,我不確定如何在我的系統中實現它。我用這個例子來指導我 - RemoveAll for ObservableCollections?如何從ObservableCollection中刪除所有選定的項目

下面是我的代碼:

Basket.xaml包含在籃

<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="270" Margin="10,170,0,0" VerticalAlignment="Top" Width="125" IsSynchronizedWithCurrentItem="False" SelectionChanged="listBox_SelectionChanged"> 
     <ListView Name ="myListView" ItemsSource="{Binding PassedData}" HorizontalAlignment="Left" VerticalAlignment="Center"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="auto"/> 
          <ColumnDefinition Width="auto"/> 
         </Grid.ColumnDefinitions> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="auto"/> 
         </Grid.RowDefinitions> 
         <TextBox Grid.Column="0" Text="{Binding Name}" IsReadOnly="True" FontSize="15" /> 
         <TextBox x:Name="quanttextBox" HorizontalAlignment="Left" Height="25" Margin="155,175,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="95" TextChanged="quanttextBox_TextChanged"/> 
         <!--<TextBox Grid.Column="1" Text="{Binding Value}" /> !--> 
        </Grid> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 

    </ListBox> 
的的ObservableCollection

private static ObservableCollection<Menu.PassedData> passedData = new ObservableCollection<Menu.PassedData>(); 
    public ObservableCollection<Menu.PassedData> PassedData 
    { 

     get { return passedData; } 
     //get{ passedData = value; } 

    bool IsSelected = true; 
    } 

列表視圖

Confirm.xaml是頁面w HICH cotains按鈕刪除所有項目

public static class ObservableCollectionExtensions 
{ 
    public static void RemoveAll<T>(this ObservableCollection<T> collection, 
                 Func<T, bool> condition) 
    { 
     for (int i = collection.Count - 1; i >= 0; i--) 
     { 
      if (condition(collection[i])) 
      { 
       collection.RemoveAt(i); 
      } 
     } 
    } 
} 

按鈕本身

 private void HyperlinkButton_Click(object sender, RoutedEventArgs e) 
    { 
     Menu.PassedData.RemoveAll(data => data.IsSelected == true); 

     Frame.Navigate(typeof(MainPage)); 

    } 

Menu.PassedData

 public event SelectionChangedEventHandler SelectionChanged; 
    public class PassedData 
    { 
     public string Name { get; set; } 
     public double Value { get; set;} 

     bool IsSelected = true; 

     internal static void RemoveAll(Func<object, bool> p) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

錯誤 enter image description here

+0

如果這樣做,錯誤occour選擇列表視圖項 作出這樣的屬性設置爲true? – DRKblade

+0

如果選中,您應該在Menu.PassedData類中有一個bool屬性進行存儲。這將是容易的。你可以遍歷事件處理程序中的項目,檢查選定的屬性。如果它是真的刪除那 – Archana

+0

你如何選擇項目? – Archana

回答

0

你應該有一個布爾屬性在Menu.PassedData稱爲IsSelected。在使用SelectionChangedEvent

private void SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      foreach (var item in e.AddedItems) 
      { 

       (item as Menu.Passed).Selected=true 
      } 
     } 
private void HyperlinkButton_Click(object sender, RoutedEventArgs e) 
     { 

     PassedData.RemoveAll(data => data.IsSelected == true); 

     } 





     public class PassedData 
     { 
      public string Name { get; set; } 
      public double Value { get; set;} 

      public bool IsSelected{get;set;} 


     } 
+0

感謝您的回答,我應該在Basket.xaml.cs中設置布爾屬性?並且它應該在公共ObservableCollection 內? –

+0

它應該在Menu.Passed類 – Archana

+0

裏面Ohh我真的沒有那個ObservableCollection的類。如果我在public之後輸入類,它會給出錯誤:expected'>'我用我的代碼的當前狀態更新了問題。 –

0

您的刪除語句c.Remove(x => x.c)不提供您在擴展中定義的lambda表達式。你應該寫類似

c.Remove(x => x.c == trueStatement) 
相關問題