C# Tie ListView items with objects
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 a allCards开发者_StackOverflow
ListView
. I have another selectedCards
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 the Card
objects what moved to the selectedCards
ListView.
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.
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);
}
}
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.
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.
精彩评论