C# Tie ListView items with objects
Solution 1
To expand on @CharithJ's answer, this is how you would 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'll need to adapt it to your own code, but that should get you started.
Solution 2
You could 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 the ObservableCollection
the ListView
automatically redraws.
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);
}
}
Solution 3
Better use ObjectListView. It is a perfect way to add and use objects with ListView. With features like Hot tracking and easy to use drag and drop your listview becomes lot simpler to manipulate.
Solution 4
1st way.
Assign the object to the Tag property of the ListViewItem. Get the tags of the selected items.
2nd Way.
Add invisible subItem to the listView that holds the ID of the Card object. Then find the card by using the selected item IDs.
hs2d
Updated on June 05, 2022Comments
-
hs2d almost 2 years
Whats the best way to tie ListView item with a object so when i move the item form one listview to another then i would be still able to tell to what object its assigned. For example, i have object
Cards
. All these are listed in aallCards
ListView
. I have anotherselectedCards
ListView
and a button what moves selected items from one listview to another. When im done my selection i need to get the list of theCard
objects what moved to theselectedCards
ListView. -
hs2d almost 13 yearsSorry, i forgot to say that im using WinForms.
-
Nolonar about 11 yearsI know this answer is old, but this was EXACTLY what I was looking for. I never knew the
Tag
property, nor what it did or what it was for, so thanks a bunch :D