C# WPF Tutorial #12 - ListView
HTML-код
- Опубликовано: 31 май 2024
- Learning the basics of the ListView control including add, delete, and clear, as well as single and multiple selection modes and how to manage several use cases and pitfalls.
Happy Coding!
Chapters:
0:00 Setup
0:19 Creating a ListView
0:56 ListView Items
1:40 Adding ListView Items from GUI
4:15 Clearing ListView
4:40 Deleting - SelectedIndex
6:45 Deleting - SelectedItem
8:51 Deleting - Multiselect
10:00 Deleting and Enumeration Info
11:37 SelectionMode (Extended/Multiple)
13:02 Next Up - Наука
Bro you are a legend. Please do not stop these
Thank you!! I have a lot more to do 👍
Man I am really fascinated with your series! your explaining way is very amazing, Thank you so much! ❤
Thank you, and thanks for watching! Let me know if you have any questions!
Loving the content. Please don't stop. Preparing for a Games tools programming internship, these videos have been the best WPF content I've found. One for other people, as I am sure you're aware, iterating the loop in reverse will allow you to modify while enumerating! Should save some cycles, GC alloc / memory usage etc.
Thanks so much! Best of luck with it!!
Great note! Yes, I have definitely used the "just make the for go backwards" method my fair share of times, but I've ran into so many various issues with that approach that I've decided the couple of extra bits are worth it. (generally with other people trying to add features without paying enough attention) Some hilarious bugs have come out of that one, though. I won't lie, I still often do it that way if it's just me on a project, though, just depends on exactly what it is!😂
Great, awesome explanations, thank you a lot. i'm web developper and your serie really helped me to understand how could i think my wpf code . THANKS :)
Thank you, very glad to help!!
i just found your channel and i think i am very lucky. 🔥
the explaining way is just perfect. please keep on this. 🔥
thx alot
Thanks so much!! Still have a lot to make!
спасибо за классные видео, надеюсь в будущем будет много таких видео
Thank you, hopefully I will be back at it soon.
This series is fantastic! Thank you so much for doing it. Will you be covering datagrids at any point? Specifically I am interested in complex formatting of datagrids.
Thank you! Yes, I will be doing DataGrid soon. Will probably be done over multiple videos as it's a bit more complex than the average control.
@@KampaPlays Great! Thanks, I'm looking forward to it.
Thank you for this tutorial
No problem!
A bug that I have found with the Deleting section is that if you have multiple items that have the same exact string, the item that is deleted is the one that is highest in the list, not necessarily the one selected.
For example:
A
B (
You are absolutely right, and I thought I mentioned this, but didn't!! -- Thank you for the comment!
Thank you Kampa!! ☺☺
Glad to help! Thank you!
For what it's worth... At around 8:30 in the video's timeline, I encountered some problems when experimenting with the executable. I used the CLR button to clear the existing lines (a,b,c) from the ListView control, and then I added the same string 'asdf' three times. I selected the second line in the ListView control and used the DEL button to delete it (selecting YES on the delete confirmation dialog). I then selected the first line in the ListView control and tried using the DEL button to delete it. This time the delete operation fails.
Within method 'btnDelete_Click' I added a watch on object 'lvEntries' and used it to monitor that object's 'SelectedIndex' and 'SelectedItem' properties. As before, I cleared the ListView control, I added 'asdf' three times, and I successfully deleted the second 'asdf' line in the ListView control. The ListView control now contains two 'asdf' lines. I select the first 'asdf' line in the ListView control and click on the DEL button. At this point the lvEntries watch indicates SelectedIndex=-1 and SelectedItem=null and the delete operation fails. If I select the second (last) 'asdf' line in the ListView control and click on the DEL button, the lvEntries watch indicates SelectedIndex=1 and SelectedItem='asdf' and the delete operation succeeds. I select the only remaining 'asdf' line in the ListView control and click on the DEL button, the lvEntries watch indicates SelectedIndex=-1 and SelectedItem=null and the delete operation fails. At this point I cannot use the DEL button to delete the remaining 'asdf' line in the ListView control.
I could not duplicate this problem when I added three different strings to the ListView control (e.g., 'qwer', 'asdf', 'zxcv').
For what it's worth, I'm using Visual Studio Community 2022 v. 17.6.2. I'm guessing this is possibly a bug in the Items.Remove() class method.
I thought I'd pass this along in case someone else encounters this problem.
Very interesting. I just tried to recreate your issue following your steps, and I couldn't reproduce the error.
The only thing that I could think of that might be happening is that once you delete the 2nd "asdf", you are not actually clicking the 1st "asdf" before pressing delete again. When you have an item selected in a ListView, and it is removed, the selection *appears* to return to the first line, but it the SelectedItem property is not actually set. IF this is the case, and you want to handle this, you could automatically set the selecteditem to the 0th index anytime an item is removed (if the collection size is greater than 0). If this isn't the case, I'm not sure what crazy bug you are seeing!!
Regardless, thank you for the information!!!
@@KampaPlays I had some free time today, so I decided to reproduce your code example from scratch to see what would happen. The result was exactly the same as I described previously. And yes, I'm expressly selecting the row I want to delete before I click the DEL button. To select the row I want to delete, I've tried using the mouse (left-clicking once on the desired row) and the keyboard (i.e., TAB key and ARROW keys). FWIW, I get this result on two different computers when using Visual Studio Community 2022 (versions: v17.6.2 and v17.6.3). I've tested the DEBUG and RELEASE builds; both behave the same. I've also tested with .NET 6.0 and .NET 7.0 as the project's "Target framework" setting, as well as different target OS versions on the project's settings page.
Hi Kampa, thank you for your very good structured Video!
Is there a simple way to select (or deselect) single items programmatically?
I have a large list, and i want a few buttons to pre-select items - and the user should be able to change this preselection.
In theory the ListItem Class has a .IsSelected property which can be set in the CodeBehind.
But i cannot type:
lvEntries.Items[0].IsSelected = true;
I tried to derive the Class used for the list items from ListViewItem, then i can set .IsSelected = true.
The selection is visible when starting the app, but then no content is visible ;-) so this cannot be the way.
If you find time to answer - thank you in advance !
Thanks!
I think what you are looking for is modifying the SelectedItems collection inherited from ListBox, rather than modifying the ListItems' IsSelected property. For example:
listView.Items.Add("HELLO0");
listView.Items.Add("HELLO1");
listView.Items.Add("HELLO2");
listView.Items.Add("HELLO3");
listView.SelectedItems.Add(listView.Items[0]);
listView.SelectedItems.Add(listView.Items[2]);
listView.SelectedItems.Add(listView.Items[3]);
listView.SelectedItems.Remove(listView.Items[2]);
This would programmatically select items 0,2,3 in the list, and then deselect 2.
Hope this helps!
@@KampaPlays Thank you! Yes, this was exactly the solution i found yesterday - i planned to update my comment, but you were faster ;-) works fine, via global variable even usable from the ViewModel Class.
How would you make the listview a folder and file tree?
You would use a TreeView control instead, they work very similarly. I'll add this control to my todos!
there is no comment called lv entries in my visual studio
lvEntries is the Name property of the ListView itself (0:50)