English 中文(简体)
C#并列列表查看带有对象的项
原标题: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 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.

最佳回答

要扩展@CharithJ的答案,以下是如何使用标记属性:

    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
        }
    }

显然,您需要将其调整为适合自己的代码,但这应该会让您开始。

问题回答

您可以使用可观察的集合,并为Card类创建一个数据模板。然后,您只需将ListView绑定到集合,它就可以为您完成所有工作。当您将项目添加到ObservableCollection时,ListView会自动重新绘制。

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);
    }
}

第一路。

将对象指定给ListViewItem的Tag属性。获取所选项目的标记。

第二条路。

将不可见的子项添加到包含Card对象ID的listView中。然后使用选定的物品ID找到卡片。

更好地使用ObjectListView。这是通过ListView添加和使用对象的完美方式。有了热跟踪和易于使用的拖放等功能,您的列表视图操作起来会简单得多。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签