Reputation: 125
I'm facing an issue where I can add items to my listview without any issues it's working perfectly but when I want to delete a item it cant do it for some reason. I want to be able to delete a item WITHOUT having to select the item, I was thinking of doing it by string like.. If LvItems.Items == "This string" then it knows which one to delete. I dont know what options are available for me with listviews so I'll ask the pros on Stack.
The problem:
How do I delete an item without having to select the item I want to be able to find the user bys tring and delete that user from the list and the refresh the UI so that user isnt in the listview anymore.
This was my thought process behind this code.
Create a listview
Add a listviewitem column
Create a list
Add something to the list on button click event handler & populate that listview with the items inside the list
refresh the listView UI
Here is the .CS
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//List<Player> items = new List<Player>();
public ObservableCollection<Player> items = new ObservableCollection<Player>();
private void btnAppend_Click(object sender, RoutedEventArgs e)
{
items.Add(new Player() { Username = "John Doe" });
lvUsers.ItemsSource = items;
lvUsers.Items.Refresh();
}
private void btnRemove_Click(object sender, RoutedEventArgs e)
{
items.Remove(new Player() { Username = "John Doe" });
lvUsers.Items.Refresh();
}
}
public class Player
{
public string Username { get; set; }
}
And here is the XAML
<Window x:Class="Listviewssssssssssssss.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Listviewssssssssssssss"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView Name="lvUsers" ItemsSource="{Binding newPatientList}" HorizontalAlignment="Left" Height="107" Margin="10,10,0,0" VerticalAlignment="Top" Width="497">
<ListView.View>
<GridView>
<GridViewColumn Header="Player" Width="120" DisplayMemberBinding="{Binding Username}" />
</GridView>
</ListView.View>
</ListView>
<TextBox Name="tbConent" HorizontalAlignment="Left" Height="78" Margin="10,122,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="497"/>
<Button Name="btnAppend" Click="btnAppend_Click" Content="Append" HorizontalAlignment="Left" Margin="44,228,0,0" VerticalAlignment="Top" Width="75"/>
<Button Name="btnRemove" Click="btnRemove_Click" Content="Remove" HorizontalAlignment="Left" Margin="371,228,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
Upvotes: 1
Views: 668
Reputation: 169420
You could select the Player
object in the ObservableCollection<Player>
using LINQ and then remove it from the collection:
private void btnRemove_Click(object sender, RoutedEventArgs e)
{
var playerToRemove = items.FirstOrDefault(x => x.Username == "John Doe");
if (playerToRemove != null)
items.Remove(playerToRemove);
}
Upvotes: 2