如何使用 ObservableCollection 从列表视图中删除 SelectedItem
How to Remove SelectedItem from a listview using ObservableCollection
我研究了 3 天,没有任何效果。
好的,所以我得到它来将项目添加到列表视图!
如何从同一个 ObservableCollection 实例中删除选定项。
在列表视图的 Xaml 中我需要什么来获取 SelectedItem?
如何在我的 RemoveModPropList 中引用 SelectedItems 并将其删除?
我的XAML
<Page.DataContext>
<m:Content/>
</Page.DataContext>
<Page.Resources>
<vm:VM_Base_ObsColl x:Key="VM_Base_ObsColl"/>
</Page.Resources>
<Grid>
<StackPanel
Background="DarkGray"
Height="500" Width="340"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Margin="10,40,10,0">
<TextBox x:Name="boxInputCategory"
Header="Category"
Text="{Binding Category, Mode=TwoWay}"
Height="60" Width="250"/>
<TextBox x:Name="boxInputTitle"
Header="Title"
Text="{Binding Title, Mode=TwoWay}"
Height="60" Width="250"/>
<TextBox x:Name="boxInputMediaType"
Header="Media Type"
Text="{Binding MediaType, Mode=TwoWay}"
Height="60" Width="250"/>
<TextBox x:Name="boxInputPlayTime"
Header="Play Time"
Text="{Binding PlayTime, Mode=TwoWay}"
Height="60" Width="250"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Lists"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="2 2"/>
<AppBarButton Icon="Add"
Content="Save All"
HorizontalAlignment="Right" Height="40"
Command="{Binding Path=Cmd_ADDModelPropsList,
Source={StaticResource VM_Base_ObsColl}}"
CommandParameter="{Binding}"/>
<AppBarButton Icon="Remove"
Content="Remove"
HorizontalAlignment="Right" Height="40"
Command="{Binding Path=Cmd_RemoveModPropsList,
Source={StaticResource VM_Base_ObsColl}}"
CommandParameter="{Binding}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="SQLite"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="2 2"/>
<AppBarButton Icon="Save"
Content="Save All"
HorizontalAlignment="Right"
Click="AppBarBtnAddText_Click"/>
</StackPanel>
<StackPanel Height="180">
<ListView x:Name="listViewOutput"
DataContext="{Binding Source={StaticResource VM_Base_ObsColl}}"
ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="70" Width="350" Margin="1 1" Background="Gray">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Category}"
Grid.Column="0" Grid.Row="0"
Height="30" Width="150"
HorizontalAlignment="Left"
Margin="2 2"/>
<TextBlock Text="{Binding Title}"
Grid.Column="1" Grid.Row="0"
Height="30" Width="150"
HorizontalAlignment="Left"
Margin="2 2"/>
<TextBlock Text="{Binding MediaType}"
Grid.Column="0" Grid.Row="1"
Height="30" Width="150"
HorizontalAlignment="Left"
Margin="2 2"/>
<TextBlock Text="{Binding PlayTime}"
Grid.Column="1" Grid.Row="1"
Height="30" Width="150"
HorizontalAlignment="Left"
Margin="2 2"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</StackPanel>
</Grid>
型号
public class Content : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Content()
{
if (DesignMode.DesignModeEnabled)
{
Category = "Human Fx";
Title = "Out the Door CheckList";
MediaType = "Speech";
PlayTime = "10:00 AM";
}
}
private void OnPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
public override string ToString()
{
return Title + " ," + Category + " ," + MediaType + " " + PlayTime;
}
#region Class/Model/Record Props & Fields
public int Id { get; set; }
private string category;
public string Category
{
get => category;
set
{
category = value;
OnPropertyChanged("Category");
}
}
private string title;
public string Title
{
get => title;
set
{
title = value;
OnPropertyChanged("Title");
}
}
private string mediaType;//Speech,Audio,Video
public string MediaType
{
get => mediaType;
set
{
mediaType = value;
OnPropertyChanged("MediaType");
}
}
private string playTime;
public string PlayTime
{
get => playTime;
set
{
playTime = value;
OnPropertyChanged("PlayTime");
}
}
private string titleSet;
public string TitleSet
{
get => titleSet;
set
{
titleSet = value;
OnPropertyChanged("TitleSet");
}
}
private string playSet;
public string PlaySet
{
get => playSet;
set
{
playSet = value;
OnPropertyChanged("PlaySet");
}
}
private bool repeatsIsOn;
public bool RepeatsIsOn
{
get => repeatsIsOn;
set
{
repeatsIsOn = value;
OnPropertyChanged("RepeatsIsOn");
}
}
#endregion
}
ViewModel - 在 RemoveModPropsList 方法中,我完全不知道如何从 ListView 中获取选定项,以及如何从 ObservableCollection 的同一实例中删除选定项。
public class VM_Base_ObsColl : ObservableCollection<Content>
{
public Cmd_ADDModelPropsList Cmd_ADDModelPropsList { get; set; }
public Cmd_RemoveModPropsList Cmd_RemoveModPropsList { get; set; }
public VM_Base_ObsColl()
{
Cmd_ADDModelPropsList = new Cmd_ADDModelPropsList(this);
Cmd_RemoveModPropsList = new Cmd_RemoveModPropsList(this);
if (DesignMode.DesignModeEnabled)
{
for (int i = 1; i < 3; i++)
{
Add(new Content()
{
Category = "Category " + 1+i,
Title = "Title " + 1+i,
MediaType = "Media Type " + 1+i,
PlayTime = "8:00 AM " + 1 + i
});
}
}
}
public void ADDModelPropsList(Content content)
{
Content c = new Content();
c.Category = content.Category;
c.Title = content.Title;
c.MediaType = content.MediaType;
c.PlayTime= content.PlayTime;
Add(c);
}
public void RemoveModPropsList(Content content)
{
Content c = new Content();
c.Category = content.Category;
c.Title = content.Title;
c.MediaType = content.MediaType;
c.PlayTime = content.PlayTime;
Remove(c);
}
用于删除所选项目的我的 ICommand 文件
public class Cmd_RemoveModPropsList : ICommand
{
public VM_Base_ObsColl ViewModel { get; set; }
public Cmd_RemoveModPropsList(VM_Base_ObsColl viewModel)
{
ViewModel = viewModel;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
ViewModel.RemoveModPropsList(parameter as Content);
}
}
您似乎正在创建一个新的 object 并试图在您对 RemoveModPropsList
的调用中删除它。 ObservableColltection
将尝试删除此 object。但它不是 collection 的一部分。因此不会删除任何内容。
解决方案是通过引用删除 object(ListView
的 SelectedItem(s)
属性)。然后你可以将 objects 的特定实例传递给 ObservableList
.
的 Remove
函数
您的问题有两种解决方案:
将 Add() 对象的相同引用传递给 Remove()
ObservableCollection<Class1> test = new ObservableCollection<Class1>();
Class1 obj = new Class1("Maria", 22203);
test.Add(obj);
test.Remove(obj); //This works
如果您无法获得相同的参考,请使用 linq。
ObservableCollection<Class1> test = new ObservableCollection<Class1>();
Class1 obj = new Class1("Maria", 22203);
test.Add(obj);
List<Class1> v = test.Where(x => x.Name == "Maria" && x.zip == 22203).ToList();
foreach (Class1 c in v)
{
test.Remove(c); //Also works
}
我研究了 3 天,没有任何效果。 好的,所以我得到它来将项目添加到列表视图! 如何从同一个 ObservableCollection 实例中删除选定项。
在列表视图的 Xaml 中我需要什么来获取 SelectedItem?
如何在我的 RemoveModPropList 中引用 SelectedItems 并将其删除?
我的XAML
<Page.DataContext>
<m:Content/>
</Page.DataContext>
<Page.Resources>
<vm:VM_Base_ObsColl x:Key="VM_Base_ObsColl"/>
</Page.Resources>
<Grid>
<StackPanel
Background="DarkGray"
Height="500" Width="340"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Margin="10,40,10,0">
<TextBox x:Name="boxInputCategory"
Header="Category"
Text="{Binding Category, Mode=TwoWay}"
Height="60" Width="250"/>
<TextBox x:Name="boxInputTitle"
Header="Title"
Text="{Binding Title, Mode=TwoWay}"
Height="60" Width="250"/>
<TextBox x:Name="boxInputMediaType"
Header="Media Type"
Text="{Binding MediaType, Mode=TwoWay}"
Height="60" Width="250"/>
<TextBox x:Name="boxInputPlayTime"
Header="Play Time"
Text="{Binding PlayTime, Mode=TwoWay}"
Height="60" Width="250"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Lists"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="2 2"/>
<AppBarButton Icon="Add"
Content="Save All"
HorizontalAlignment="Right" Height="40"
Command="{Binding Path=Cmd_ADDModelPropsList,
Source={StaticResource VM_Base_ObsColl}}"
CommandParameter="{Binding}"/>
<AppBarButton Icon="Remove"
Content="Remove"
HorizontalAlignment="Right" Height="40"
Command="{Binding Path=Cmd_RemoveModPropsList,
Source={StaticResource VM_Base_ObsColl}}"
CommandParameter="{Binding}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="SQLite"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="2 2"/>
<AppBarButton Icon="Save"
Content="Save All"
HorizontalAlignment="Right"
Click="AppBarBtnAddText_Click"/>
</StackPanel>
<StackPanel Height="180">
<ListView x:Name="listViewOutput"
DataContext="{Binding Source={StaticResource VM_Base_ObsColl}}"
ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="70" Width="350" Margin="1 1" Background="Gray">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Category}"
Grid.Column="0" Grid.Row="0"
Height="30" Width="150"
HorizontalAlignment="Left"
Margin="2 2"/>
<TextBlock Text="{Binding Title}"
Grid.Column="1" Grid.Row="0"
Height="30" Width="150"
HorizontalAlignment="Left"
Margin="2 2"/>
<TextBlock Text="{Binding MediaType}"
Grid.Column="0" Grid.Row="1"
Height="30" Width="150"
HorizontalAlignment="Left"
Margin="2 2"/>
<TextBlock Text="{Binding PlayTime}"
Grid.Column="1" Grid.Row="1"
Height="30" Width="150"
HorizontalAlignment="Left"
Margin="2 2"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</StackPanel>
</Grid>
型号
public class Content : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Content()
{
if (DesignMode.DesignModeEnabled)
{
Category = "Human Fx";
Title = "Out the Door CheckList";
MediaType = "Speech";
PlayTime = "10:00 AM";
}
}
private void OnPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
public override string ToString()
{
return Title + " ," + Category + " ," + MediaType + " " + PlayTime;
}
#region Class/Model/Record Props & Fields
public int Id { get; set; }
private string category;
public string Category
{
get => category;
set
{
category = value;
OnPropertyChanged("Category");
}
}
private string title;
public string Title
{
get => title;
set
{
title = value;
OnPropertyChanged("Title");
}
}
private string mediaType;//Speech,Audio,Video
public string MediaType
{
get => mediaType;
set
{
mediaType = value;
OnPropertyChanged("MediaType");
}
}
private string playTime;
public string PlayTime
{
get => playTime;
set
{
playTime = value;
OnPropertyChanged("PlayTime");
}
}
private string titleSet;
public string TitleSet
{
get => titleSet;
set
{
titleSet = value;
OnPropertyChanged("TitleSet");
}
}
private string playSet;
public string PlaySet
{
get => playSet;
set
{
playSet = value;
OnPropertyChanged("PlaySet");
}
}
private bool repeatsIsOn;
public bool RepeatsIsOn
{
get => repeatsIsOn;
set
{
repeatsIsOn = value;
OnPropertyChanged("RepeatsIsOn");
}
}
#endregion
}
ViewModel - 在 RemoveModPropsList 方法中,我完全不知道如何从 ListView 中获取选定项,以及如何从 ObservableCollection 的同一实例中删除选定项。
public class VM_Base_ObsColl : ObservableCollection<Content>
{
public Cmd_ADDModelPropsList Cmd_ADDModelPropsList { get; set; }
public Cmd_RemoveModPropsList Cmd_RemoveModPropsList { get; set; }
public VM_Base_ObsColl()
{
Cmd_ADDModelPropsList = new Cmd_ADDModelPropsList(this);
Cmd_RemoveModPropsList = new Cmd_RemoveModPropsList(this);
if (DesignMode.DesignModeEnabled)
{
for (int i = 1; i < 3; i++)
{
Add(new Content()
{
Category = "Category " + 1+i,
Title = "Title " + 1+i,
MediaType = "Media Type " + 1+i,
PlayTime = "8:00 AM " + 1 + i
});
}
}
}
public void ADDModelPropsList(Content content)
{
Content c = new Content();
c.Category = content.Category;
c.Title = content.Title;
c.MediaType = content.MediaType;
c.PlayTime= content.PlayTime;
Add(c);
}
public void RemoveModPropsList(Content content)
{
Content c = new Content();
c.Category = content.Category;
c.Title = content.Title;
c.MediaType = content.MediaType;
c.PlayTime = content.PlayTime;
Remove(c);
}
用于删除所选项目的我的 ICommand 文件
public class Cmd_RemoveModPropsList : ICommand
{
public VM_Base_ObsColl ViewModel { get; set; }
public Cmd_RemoveModPropsList(VM_Base_ObsColl viewModel)
{
ViewModel = viewModel;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
ViewModel.RemoveModPropsList(parameter as Content);
}
}
您似乎正在创建一个新的 object 并试图在您对 RemoveModPropsList
的调用中删除它。 ObservableColltection
将尝试删除此 object。但它不是 collection 的一部分。因此不会删除任何内容。
解决方案是通过引用删除 object(ListView
的 SelectedItem(s)
属性)。然后你可以将 objects 的特定实例传递给 ObservableList
.
Remove
函数
您的问题有两种解决方案:
将 Add() 对象的相同引用传递给 Remove()
ObservableCollection<Class1> test = new ObservableCollection<Class1>(); Class1 obj = new Class1("Maria", 22203); test.Add(obj); test.Remove(obj); //This works
如果您无法获得相同的参考,请使用 linq。
ObservableCollection<Class1> test = new ObservableCollection<Class1>(); Class1 obj = new Class1("Maria", 22203); test.Add(obj); List<Class1> v = test.Where(x => x.Name == "Maria" && x.zip == 22203).ToList(); foreach (Class1 c in v) { test.Remove(c); //Also works }