当我点击不同XAML中的按钮时,我试图从ObservableCollection中删除所有选定的项目,我不确定如何在我的系统中实现它。我使用此示例来指导我 - RemoveAll for ObservableCollections?
以下是我的代码:
Basket.xaml,其中包含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
<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>
确认.xaml是包含删除所有项目的按钮的页面
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();
}
}
答案 0 :(得分:0)
您的删除语句c.Remove(x => x.c == trueStatement)
未提供您在扩展程序中定义的lambda表达式。你应该写点像
function attachEvent(element, event, callbackFunction) {
if(element.addEventListener) {
element.addEventListener(event, function(){
callbackFunction(this.getAttribute("src"));
}, false);
}
}
//start the function
attachEvent(".Dicon", "click", handler);
function handler(){
console.log("stop");
}
答案 1 :(得分: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;}
}