当我动态创建一个列表框时,这很有用..但现在我正在通过WPF严格制作所有内容..这似乎不起作用,我不知道为什么......这对我来说似乎是对的,我觉得也许我错过了什么?
我得到的错误是“对象引用未设置为对象的实例”
wpf代码:
<ListBox x:Name="ListBoxT10">
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<TextBox FontSize="14" Text="" Width="120"/>
<TextBox FontSize="14" Text="" Width="40" MaxLength="3"/>
<TextBox FontSize="14" Text="" Width="413"/>
</StackPanel>
</ListBoxItem>
...a bunch of the same listboxitems...
C#代码:
int a = 0;
foreach (var item in ListBoxT10.Items)
{
StackPanel tempStackPanel = item as StackPanel;
Console.WriteLine(tempStackPanel.Children.Count); //this even errors.. i guess it's not finding the children
string pName = (tempStackPanel.Children[0] as TextBox).Text; //error
string tri = (tempStackPanel.Children[1] as TextBox).Text; //error
string stats = (tempStackPanel.Children[2] as TextBox).Text; //error
编辑:我意识到这是因为ListBoxT10.Items中的项目实际上是“ListBoxItem”的...但是现在我无法获得listboxitems的子项... ugh = [
答案 0 :(得分:0)
ListBoxItem lbi = (ListBoxItem)(ListBoxT10.ItemContainerGenerator.ContainerFromIndex(a));
StackPanel sp = lbi.Content as StackPanel;
string pName = (sp.Children[1] as TextBox).Text;
string tri = (sp.Children[2] as TextBox).Text;
string stats = (sp.Children[3] as TextBox).Text;
我忘记了listboxitem .........并不认为那会注册为对象