C # TIE ListView items with objects

What is the best way to associate a ListView item with an object, so when I move an item from one list to another, I can still tell which object is assigned to it. For example, I have a Cards object. All of them are listed in allCards ListView . I also have selectedCards ListView and a button that moves the selected items from one list to another. When I made my choice, I need to get a list of Card objects that have been moved to selectedCards ListView.

+6
source share
4 answers

To extend the answer to @CharithJ, you should use the tag property:

  ListView allCardsListView = new ListView(); ListView selectedCardsListView = new ListView(); List<Card> allCards = new List<Card>(); List<Card> selectedCards = new List<Card>(); public Form1() { InitializeComponent(); foreach (Card selectedCard in selectedCards) { ListViewItem item = new ListViewItem(selectedCard.Name); item.Tag = selectedCard; selectedCardsListView.Items.Add(item); } foreach (Card card in allCards) { ListViewItem item = new ListViewItem(card.Name); item.Tag = card; allCardsListView.Items.Add(new ListViewItem(card.Name)); } Button button = new Button(); button.Click += new EventHandler(MoveSelectedClick); } void MoveSelectedClick(object sender, EventArgs e) { foreach (ListViewItem item in allCardsListView.SelectedItems) { Card card = (Card) item.Tag; //Do whatever with the card } } 

Obviously, you will need to adapt it to your own code, but this should help you get started.

+5
source

You can use observable collections and create a datatemplate for your Card class. Then you just bind your ListView to the collection, and it does all the work for you. When you add an item to an ObservableCollection , the ListView automatically redrawn.

 using System.Collections.ObjectModel; <ListView Name="allCardsView" Source="{Binding}"> <ListView.ItemTemplate> <DataTemplate DataType="{x:Type yourXmlns:Card}"> //Your template here </DataTemplate> </ListView.ItemTemplate> </ListView> <ListView Name="selectedCardsView" Source="{Binding}"> <ListView.ItemTemplate> <DataTemplate DataType="{x:Type yourXmlns:Card}"> //Your template here </DataTemplate> </ListView.ItemTemplate> </ListView> ObservableCollection<Card> allCards = new ObservableCollection<Card>(); ObservableCollection<Card> selectedCards = new ObservableCollection<Card>(); allCardsView.DataContext = allCards; selectedCardsView.DataContext = selectedCards; public void ButtonClickHandler(object sender, EventArgs e) { if (allCardsView.SelectedItem != null && !selectedCards.Contains(allCardsView.SelectedItem)) { selectedCards.Add(allCardsView.SelectedItem); } } 
+5
source

The first way.

Assign the object to the Tag property in the ListViewItem element. Get tags for selected items.

Second way.

Add an invisible subItem to the listView containing the identifier of the Card object. Then find the map using the identifiers of the selected items.

+1
source

Better to use an ObjectListView . This is a great way to add and use objects with a ListView. With features like Hot Tracking and easy drag and drop, your list is much easier to manipulate.

+1
source

Source: https://habr.com/ru/post/889453/


All Articles