C# 以编程方式将 ListViewItem 添加到 WPF 中的 Listview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/665234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add programmatically ListViewItem to Listview in WPF
提问by
Possible Duplicate:
WPF ListView - how to add items programmatically?
How it can be done in C#?
如何在 C# 中完成?
回答by MrTelly
If you're using the ListView as it is intended it will be bound to an underlying list of some kind of Object, and that class should implement INotifyChanged. In WPF you don't directly add/remove items from the ListView, you deal with the bound list structure, and it notifies the UI of the change, which then cleverly redraws itself with the new items.
如果您按预期使用 ListView,它将绑定到某种对象的底层列表,并且该类应实现 INotifyChanged。在 WPF 中,您不直接在 ListView 中添加/删除项目,而是处理绑定列表结构,并通知 UI 更改,然后 UI 巧妙地使用新项目重新绘制自己。
回答by Robert Macnee
This is how you would add a ListViewItem
created in code to your ListView
:
这是您将ListViewItem
代码中创建的添加到您的ListView
:
myListView.Items.Add(new ListViewItem { Content = "This is an item added programmatically." });
However, I agree with MrTellythat this shouldn't be necessary, you should be setting ListView.ItemsSource
to some collection rather than manipulating ListView.Items
directly.
但是,我同意MrTelly 的观点,这应该没有必要,您应该设置ListView.ItemsSource
一些集合而不是ListView.Items
直接操作。
If you give us more details about what you want to accomplish maybe we can help you do it the WPF way, which is not always the easy way at first, but it's mucheasier in the long run.
如果您向我们提供有关您想要完成的任务的更多详细信息,也许我们可以帮助您以 WPF 方式完成,这起初并不总是容易的方法,但从长远来看会容易得多。
回答by Tawani
You can add columns dynamically to a ListView by using Attached Properties. Check out this article on the CodeProjectit explains exactly that...
您可以使用附加属性将列动态添加到 ListView。查看CodeProject上的这篇文章,它准确地解释了……
回答by JohnIdol
回答by Jimmy Lyke
Adding it directly to the ListView in your App isn't necessarily the "WPF-way". Consider this:
将其直接添加到应用程序中的 ListView 不一定是“WPF 方式”。考虑一下:
public class BindableListViewModel
{
public IList<TypeOfObjectToDisplay> AllObjectsToDisplay;
public ICommand AddNewItemToList;
public BindableListViewModel()
{
AllObjectsToDisplay = new ObservableList<TypeOfObjectToDisplay>();
AddNewItemToList = new RelayCommand(AddNewItem(), CanAddNewItem());
}
public bool CanAddNewItem()
{
//logic that determines IF you are allowed to add
//For now, i'll just say that we can alway add.
return true;
}
public void AddNewItem()
{
AllObjectsToDisplay.Add(new TypeOfObjectToDisplay());
}
}
Then, in XAML all that we need to do is bind the ItemsSource of our ListView to our AllObjectsToDisplay list. This allows us to separate the dependency of adding objects directly to our ListView; we can us WPF's strength of Data Binding to remove the direct dependencyon HOW we add businesss objects to our UI container we display them in -- a very useful practice!
然后,在 XAML 中,我们需要做的就是将 ListView 的 ItemsSource 绑定到我们的 AllObjectsToDisplay 列表。这让我们可以分离直接向我们的 ListView 添加对象的依赖;我们可以利用 WPF 的数据绑定优势来消除对如何将业务对象添加到显示它们的 UI 容器的直接依赖——这是一个非常有用的实践!
回答by Michael Olesen
(sorry, can't make comments yet)
(抱歉,还不能发表评论)
As MrTelly is on to...
由于泰利先生正在...
Bind the listview to a ObservableCollection
将列表视图绑定到 ObservableCollection
ObservableCollection<MyClassItem> lvList = new ObservableCollection<MyClassItem>();
myListview.ItemSource = lvList;
// Add an item
lvList.Add(new MyClassItem("firstname", "lastname"));
That way it will automatically update the UI when changes are made to the collection.
这样,当对集合进行更改时,它将自动更新 UI。