我試圖從不同的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();
}
}
如果這樣做,錯誤occour選擇列表視圖項 作出這樣的屬性設置爲true? – DRKblade
如果選中,您應該在Menu.PassedData類中有一個bool屬性進行存儲。這將是容易的。你可以遍歷事件處理程序中的項目,檢查選定的屬性。如果它是真的刪除那 – Archana
你如何選擇項目? – Archana