C# 如何获取列表框中的项目数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12013792/
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
how to get number of items in listbox
提问by user1563019
In my program i took a listbox, in this listbox I added number of items.
在我的程序中,我使用了一个列表框,在这个列表框中我添加了一些项目。
Now I want to know how to get number of listbox items & show number of items in a textbox. when number of items is not fixed.
现在我想知道如何获取列表框项目的数量并在文本框中显示项目的数量。当项目数量不固定时。
采纳答案by M. Mennan Kara
Let's say your listbox is called ListBox1 and textbox is TextBox1. Then the code you need is
假设您的列表框名为 ListBox1,而文本框为 TextBox1。那么你需要的代码是
TextBox1.Text = ListBox1.Items.Count.ToString();
Of course knowing your platform (WinForms, WebForms, Html etc.) would help a lot for a more accurate answer.
当然,了解您的平台(WinForms、WebForms、Html 等)将有助于获得更准确的答案。
回答by TT7
I would like to present an answer for the people who uses WPF.
我想为使用 WPF 的人提供一个答案。
<ListBox ItemsSource="{Binding Path=Parts}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Label Content="{Binding CurrentPart, Mode=OneWay}" />
<Label Content="{Binding Path=Parts.Count}"
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

