简体   繁体   English

如何在WPF中添加ListBox项目?

[英]How to add ListBox Items in WPF?

how do I add items to a listbox control by using < ListBox.ItemTemplate> ? 如何使用<ListBox.ItemTemplate>将项目添加到列表框控件?

here is the xaml part: 这是xaml部分:

<ListBox HorizontalAlignment="Stretch" Name="ListBox1" VerticalAlignment="Stretch" Margin="0,20">
 <ListBox.ItemTemplate>
  <DataTemplate>
   <Label Content="???????" />
  </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

and here is the code for adding items: 这是添加项目的代码:

private sub Button1_Click() Handles Button1.Click
ListBox1.Items.Add("Hello World")
End Sub

if I click on the buton1, a list item will be added with "??????" 如果我单击buton1,则会在列表项中添加“ ??????” - I need to replace the "?????" -我需要替换“ ?????” with {Binding} or something so it can get the value correct value from button1_click ("Hello World") {Binding}之类的东西,以便可以从button1_click(“ Hello World”)中获取正确的值

Yes, just replace it with {Binding} , that alone should do it. 是的,只需将其替换为{Binding} ,那应该一个人做。

( {Binding} binds to the DataContext and the DataContext of the ItemTemplate will be the respective item, if the item is a simple string as in your example that will do, if it is a complex data object you will want to specify a Path to bind to a property of the item) {Binding}绑定到DataContextDataContext的的ItemTemplate将各自的项目,如果该项目是一个简单的字符串作为你的榜样,会做,如果它是一个复杂的数据对象,你会想指定Path来绑定到项目的属性)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM