如何设置 ItemSource 和 ItemTemplate 以显示对象列表
我有一个列表框,我想显示对象列表,我遵循 MVVM 模式,但发现很难实现我想要的。
MainWindowView.xaml
<ListBox ItemsSource="{Binding Path=MyList}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
MainWindowViewModel.cs
private List<ListBoxItem> _myList = new List<ListBoxItem>();
public List<ListBoxItem> MyList
{
get { return _myList ; }
set
{
_myList = value;
OnPropertyChanged("MyList");
}
}
public SprintBacklogViewModel()
{
foreach(MyObject obj in MyObjects.MyObjectList)
{
ListBoxItem item = new ListBoxItem();
item.Content = obj;
MyList.Add(item);
}
}
MyList 已正确更新,但窗口中未显示任何内容。 (ItemsSource="{Binding Path=MyList}" 也有效,我使用不同的数据进行了测试) 我之前没有使用过 ItemTemplate,因此欢迎任何指点。我的理解是,如果我设置正确,它将显示我的对象中的数据。 例如:
<Label Content="{Binding Path=Name}"/>
MyObject 中有一个名为 Name 的属性,我想将其显示为我的列表中的标签
*编辑 在我的窗口中,我得到一行文本 - mynamespace.MyObject
I have a List box that I want to display a list of objects, I am following the MVVM pattern and am finding it difficult to achieve what I want.
MainWindowView.xaml
<ListBox ItemsSource="{Binding Path=MyList}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
MainWindowViewModel.cs
private List<ListBoxItem> _myList = new List<ListBoxItem>();
public List<ListBoxItem> MyList
{
get { return _myList ; }
set
{
_myList = value;
OnPropertyChanged("MyList");
}
}
public SprintBacklogViewModel()
{
foreach(MyObject obj in MyObjects.MyObjectList)
{
ListBoxItem item = new ListBoxItem();
item.Content = obj;
MyList.Add(item);
}
}
MyList is getting updated correctly, but nothing is displaying in the window. (ItemsSource="{Binding Path=MyList}" also works, I tested with different data) I have not used an ItemTemplate before so any pointers are welcome. My understanding of it is that If I set it up correctly It will display the data in my objects.
eg:
<Label Content="{Binding Path=Name}"/>
there is a property in MyObject called Name, I want to display this as a label in my list
*EDIT
In my window I get a line of text - mynamespace.MyObject
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ViewModel 中的 MyList 属性是 ListBoxItem 类型的属性,它具有属性 名称 但它不是名称我的对象。 更改 ViewModel 中的属性
因此,您需要通过Replace
with
MyList property in ViewModel is property of type ListBoxItem, it has property Name but it's not Name of MyObject. So you need to change your property in your ViewModel by
Replace
with
如果您绑定到
ListBoxItems
列表(ListBox数据绑定) code> 将忽略
ItemTemplate
并仅使用适合ListBox
预期容器的项目。容器将自动生成,您不需要在列表中执行此操作。如果您在运行时向集合添加项目,则需要通知绑定引擎更新更改,为此您应该使用
ObservableCollection
或任何实现INotifyCollectionChanged
。 (这样做时,您通常会进一步使字段只读
并且只提供一个getter)这就是没有项目的原因。Your list should not contain UI-Elements but data (you are data-binding), if you bind to a list of
ListBoxItems
theListBox
will disregard theItemTemplate
and just use the items as they fit the expected container for theListBox
. Containers will be generated automatically, you do not need to do that in your list.If you add items to a collection at runtime the binding engine needs to be notified to update the changes, for this you should use an
ObservableCollection
or anything that implementsINotifyCollectionChanged
. (When doing so you further usually make the fieldreadonly
and only provide a getter) This is the reason why there are no items.