在我的程序中,我使用了一个列表框,在这个列表框中添加了许多项目。
现在我想知道如何获取列表框项目的数量,并在文本框中显示项目数,当项目数不固定时。
现在我想知道如何获取列表框项目的数量,并在文本框中显示项目数,当项目数不固定时。
假设你的列表框名为 ListBox1,文本框名为 TextBox1。那么你需要的代码是:
TextBox1.Text = ListBox1.Items.Count.ToString();
当然,了解您所使用的平台(WinForms、WebForms、Html等)会对获得更准确的答案有很大帮助。
<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>