0

I'm trying to remove all the selected items from the ObservableCollection when a button in a different XAML is clicked, I am unsure on how I can implement it in my system. I'm using this example to guide me - RemoveAll for ObservableCollections?

Below is my code:

Basket.xaml which contains the 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;
    }

Listview in Basket

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

Confirm.xaml is the page which cotains the button to remove all the items

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

The button itself

       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();
        }
    }

Error enter image description here

Community
  • 1
  • 1

2 Answers2

0

Your Remove statement c.Remove(x => x.c) does not provide the lambda expression you defined in your extension. You should write something like

c.Remove(x => x.c == trueStatement)
Wicher Visser
  • 1,513
  • 12
  • 21
0

You should have a Boolean property in Menu.PassedData called IsSelected. Make that property to true when listview item is selected using 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;}


        }
Archana
  • 3,213
  • 1
  • 15
  • 21
  • Thanks for the answer, Where should I set the boolean property in the Basket.xaml.cs? and should it be inside the public ObservableCollection? – Joe Goldberg Apr 15 '16 at 13:27
  • It should be inside Menu.Passed class – Archana Apr 15 '16 at 13:28
  • Ohh I dont really have a class for that ObservableCollection. If I input class after Public it gives an error of : expected '>' I have updated the question with the current state of my code. – Joe Goldberg Apr 15 '16 at 13:46
  • Then what is Menu.Passed? – Archana Apr 15 '16 at 13:50
  • What is the type of item of collection? – Archana Apr 15 '16 at 13:52
  • Sorry my bad yes it is a class I have set the boolean property inside that class and still nothing. – Joe Goldberg Apr 15 '16 at 13:53
  • Did you handle Selectionchanged event? What are you getting now? – Archana Apr 15 '16 at 14:10
  • Are you getting exception? – Archana Apr 15 '16 at 14:11
  • Yes when I click the button it freezes ands gives that error. Also I dont think I handle Selectionchanged event correctly. Sorry I am new to c# and just trying to get through it. question is updated with error – Joe Goldberg Apr 15 '16 at 14:26
  • You don't need RemoveAll method which is there inside PassedData. Remove that. It should call ObservableCollectionExtensions class RemoveAll method. I don't know did you write that or not. And make Selecteditem property public with getter setter – Archana Apr 15 '16 at 14:34
  • Please post the chat discussion link, which I don't have access to, since comments are getting increased – Archana Apr 15 '16 at 14:35
  • I would but unfortunately stack doesnt let me, I require more point :(. I have done exactly what you said but an error occurs in the button underneath the RemoveAll part because I deleted the automatically created method, It doesnt seem to call the ObservableCollectionExtensions because I set it in the top (Before the Menu Class ) because if I set it within the Menu Class it shows an error saying ' Extension Methods must be defined in a top level static class; ObservableCollectionExtensions is a nested class. – Joe Goldberg Apr 15 '16 at 14:57
  • Add the separate class file and add this extension class code over there – Archana Apr 15 '16 at 15:00
  • Ohhh Yes Thankyouuu :) – Joe Goldberg Apr 15 '16 at 19:38