什么是将ListView项目与对象绑定的最佳方式,所以当我将项目移动到另一个列表视图时,我仍然能够告诉它分配了哪个对象。 例如,我有对象Cards
。所有这些都列在allCards
ListView
。我有另一个selectedCards
ListView
和一个按钮,将选定的项目从一个列表视图移动到另一个列表视图。当我完成我的选择时,我需要获得移动到selectedCards
ListView的Card
对象列表。C#绑定带对象的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
}
}
很显然,你需要以使其适应自己的代码,但是这应该让你开始。
我知道这个答案是旧的,但这完全是***我正在寻找的东西。我从来不知道“Tag”属性,也不知道它做了什么或者做了什么,所以非常感谢:D – Nolonar 2013-03-25 13:34:52
第一种方式。
将对象分配给ListViewItem的Tag属性。获取所选项目的标签。
第二种方式。
将不可见子项添加到包含Card对象ID的listView中。然后使用选择的项目ID找到该卡。
您可以使用可观察的集合,并为您的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即时通讯。 – hs2d 2011-05-31 16:37:05
更好地使用ObjectListView。这是通过ListView添加和使用对象的完美方式。使用热跟踪和易于使用的功能拖放您的列表视图变得更容易操作。
- 1. 获取绑定到Kendo项目的对象ListView
- 2. 将C#对象绑定到ListView /控件
- 3. 绑定项目对象的ItemTemplate在MVVMCross
- 4. 获取绑定到每个列表项的项目和对象剑道的ListView
- 5. ListVIew中的动态绑定项目
- 6. Android - 带有特定ID的ListView项目
- 7. 验证规则对ListView的绑定项目
- 8. 将ListView项目的子项目绑定到另一个数据
- 9. ListView C#WPF绑定
- 10. Wpf Listview绑定对象没有变化
- 11. C#:如何绑定Button.Enabled是否有任何项目选择ListView
- 12. ListView到ListView绑定与项目上的转换器
- 13. 如何将用于绑定的ListView项目返回到ListView
- 14. C#。将对象绑定到对象
- 15. 绑定对象DataGridView C#
- 16. C#ListView子项目
- 17. ListView setOnItemClickListener对特定项目的操作
- 18. WinForms ListView - 从选定的字符串项目获取源对象
- 19. 从ListView中获取与SimpleCursorAdapter绑定的选定项目
- 20. 如何获得Listview绑定后的选定项目?
- 21. UWP - ListView绑定在C++/CX
- 22. 定制的ListView绑定到对象列表[的WinForms]
- 23. 基于对象数据自定义Listview项目
- 24. 将对象的子项绑定到TextBox
- 25. 按对象列表中的项目(Sitecore项目)分组,并与Repeater绑定
- 26. 添加带回路的listview子项目
- 27. 展开带有动画的ListView项目
- 28. 带有可折叠项目的ListView?
- 29. 将绑定的对象直接传递到DataTemplate项目
- 30. 如何将ListBox的项目绑定到实例对象列表?
这个问题不是很清楚。如果你将这些对象移动到'selectedCards',是不是你想要的列表? – 2011-05-31 14:02:28
@Steven:在ListView中只有卡片的名称.. – hs2d 2011-05-31 14:05:54
只是一个小建议,如果你将所有卡片重新命名为availableCards *,它会更干净... ... – 2011-05-31 14:08:15