加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#使用对象绑定ListView项

发布时间:2020-12-15 03:53:25 所属栏目:百科 来源:网络整理
导读:什么是将ListView项目与对象绑定的最佳方式,因此当我将项目从一个列表视图移动到另一个列表视图时,我仍然能够告诉它分配了什么对象. 例如,我有对象卡.所有这些都列在allCards ListView中.我有另一个selectedCards ListView和一个按钮,将所选项目从一个列表视
什么是将ListView项目与对象绑定的最佳方式,因此当我将项目从一个列表视图移动到另一个列表视图时,我仍然能够告诉它分配了什么对象.
例如,我有对象卡.所有这些都列在allCards ListView中.我有另一个selectedCards ListView和一个按钮,将所选项目从一个列表视图移动到另一个列表视图.当我完成我的选择时,我需要获取移动到selectedCards ListView的Card对象列表.

解决方法

要扩展@ 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
        }
    }

显然你需要根据自己的代码进行调整,但这应该让你开始.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读