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