Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6715835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:39:43+00:00 2026-05-26T08:39:43+00:00

I have a ListView with a specific ItemTemplate, which visually displays the contents of

  • 0

I have a ListView with a specific ItemTemplate, which visually displays the contents of a list of objects. I wanted to allow the user to select such an object by clicking on it, and as a consequence of selecting it, it will be loaded as the application’s current object and deleted from the list.

The problem is that the listview doesn’t support Click events, so I tried using the SelectionChanged event. Well, this isn’t ok as well, because if I click on an item (but without releasing the mouse) and then I move the mouse over another item (while keeping the mouse pressed), it deletes that item as well.

Any suggestions on how I can delete the selected item from the list without triggering additional operations? here’s a simplified version of my code (which has the same behaviour):

<Window x:Class="ListViewIssue.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="50"></RowDefinition>
        </Grid.RowDefinitions>
        <ListView x:Name="lstNumbers"  SelectionMode="Single"  ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionChanged="lstNumbers_SelectionChanged">
            <ListView.ItemTemplate>                
                <DataTemplate>
                    <Grid Width="170" Background="Transparent">                        
                        <TextBlock HorizontalAlignment="Left" Text="{Binding val, Mode=TwoWay}" TextWrapping="Wrap" Margin="3"/>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button Name="btnAddItem" Grid.Row="1" Click="btnAddItem_Click">Add Item</Button>
    </Grid>
</Window>

and code behind:

public partial class MainWindow : Window
    {
        public class valueClass
        {
            public int val { get; set; }
        }
        private List<valueClass> list;
        private int counter;

        public MainWindow()
        {
            InitializeComponent();
            CreateList();
            DisplayList();
        }

        private void CreateList()
        {
            list = new List<valueClass>();
            counter = 8;
            for (int i = 0; i < counter; i++)
            {
                list.Add(new valueClass() { val = i + 1 });
            }
        }

        private void lstNumbers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstNumbers.SelectedIndex != -1)
            {
                list.Remove(lstNumbers.SelectedValue as valueClass);
                DisplayList();
            }
        }

        private void DisplayList()
        {
            this.lstNumbers.ItemsSource = null;
            this.lstNumbers.ItemsSource = list;
        }

        private void btnAddItem_Click(object sender, RoutedEventArgs e)
        {
            list.Add(new valueClass() { val = counter++ + 1 });
            DisplayList();
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:39:43+00:00Added an answer on May 26, 2026 at 8:39 am

    I put this in an answer so it stands out from the comment wall there.

    Since you are removing on selectionchanged, anytime that changes, you’re deleting. Try using your remove logic in PreviewMouseUp and then grabbing the item there. That way, clicking down on something won’t remove the item until you’re click is finished.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListView on a page that displays a list of widgets. When
I have created a specific List which exists out of the following elements to
I have a ListView which displays information about some entities (all of the same
In have a list of objects bound to a ListView that is used to
I have a ListView which is bind dynamically to a list of object of
I have two screens the one which list the records in a listview ListView
I have ListView in my project which has ListItems as in the form of
I have ListView which is saving all data to database. For adding i have
I have a ListView which sometimes I need to put around 10000 items in.
I have a ListView inside of an Update Panel and wanted to change the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.