【问题标题】:C# Tie ListView items with objectsC# 将 ListView 项与对象联系起来
【发布时间】:2011-05-31 13:52:46
【问题描述】:

将 ListView 项与对象联系起来的最佳方法是什么,所以当我将项从一个列表视图移动到另一个列表视图时,我仍然能够告诉它分配给哪个对象。 例如,我有对象Cards。所有这些都列在allCardsListView 中。我有另一个 selectedCards ListView 和一个按钮,它将所选项目从一个列表视图移动到另一个。当我完成我的选择后,我需要获取移动到 selectedCards ListView 的 Card 对象列表。

【问题讨论】:

  • 这个问题不是很清楚。如果你把那些对象移到selectedCards,那不就是你想要的列表吗?
  • @Steven:在 ListView 中只有卡片的名称..
  • 只是一个小建议,如果你将 allCards* 重命名为 availableCards* 会更简洁...
  • C# 中的对象是通过引用传递的,因此当您将项目从一个列表移动到下一个列表时,仍应将其识别为同一对象。您是否可能在第二个列表中创建一个新实例?如果您创建一个新实例,您的对象将必须实现 IComparable 接口,以便您可以找到相等性。为了确定你的意思,你应该更好地解释。

标签: c# winforms listview


【解决方案1】:

为了扩展@CharithJ 的答案,这就是您使用 tag 属性的方式:

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

显然,您需要根据自己的代码对其进行调整,但这应该可以帮助您入门。

【讨论】:

  • 我知道这个答案很旧,但这正是我想要的完全。我从来不知道Tag 属性,也不知道它的作用或用途,所以非常感谢:D
【解决方案2】:

您可以使用可观察的集合,并为您的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);
    }
}

【讨论】:

  • 对不起,我忘了说我正在使用 WinForms。
【解决方案3】:

第一种方式。

将对象分配给 ListViewItem 的 Tag 属性。获取选中项的标签。

第二条路。

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

【讨论】:

    【解决方案4】:

    最好使用ObjectListView。这是通过 ListView 添加和使用对象的完美方式。借助热门跟踪和易于使用的拖放等功能,您的列表视图变得更易于操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      相关资源
      最近更新 更多