C# 如何将数组绑定到数据模板-列表框

C# 如何将数组绑定到数据模板-列表框,c#,binding,linq-to-xml,C#,Binding,Linq To Xml,我目前正在解析一个HTML,以生成一个列表框中显示的数组 我将数组中的每个对象输出到一个文本框,如下所示 //first line flight1.Text = node[0][2]; origin1.Text = node[0][3]; status1.Text = node[0][7]; time1.Text = node[0][6]; //second line textBlock1.Text = node[1][3]; textBlock2.Text = node[1][3]; t

我目前正在解析一个HTML,以生成一个列表框中显示的数组

我将数组中的每个对象输出到一个文本框,如下所示

//first line
flight1.Text = node[0][2];
origin1.Text = node[0][3];
status1.Text = node[0][7];
time1.Text = node[0][6];

//second line

textBlock1.Text = node[1][3];
textBlock2.Text = node[1][3];
textBlock4.Text = node[1][7];
textBlock3.Text = node[1][6];
这就是结果

正如你所想象的,这是一项相当艰苦的工作,可能不是最好的方式

是否仍然需要绑定数组中的每个部分,例如。。将所有航班名称添加到数据模板中的列表框中,以便自动列出所有航班时间和所有航班时间等

类似于XAML的东西

<ListBox Margin="6,6,-12,0" Name="listBox1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17" Width="432" Height="Auto">

                            <TextBlock Text="{Binding FlightNumber}" Foreground="#FF4BCCF5" FontSize="24" />
                            <TextBlock Text="{Binding Origin}" TextWrapping="Wrap" FontSize="22" Foreground="#FF969696" />
                            <TextBlock Text="{Binding Time}" TextWrapping="Wrap" FontSize="20" Foreground="#FF05C16C" />
                            <TextBlock Text="{Binding Status}" TextWrapping="Wrap" FontSize="20" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

我不确定您想要实现什么。但总的来说,我建议采取以下步骤:

  • 解析HTML
  • 将每一行放入
    FlightInformation
    -类中(使用与XAML示例中类似的属性)
  • 将所有
    FlightInformation
    -对象放入
    可观察集合中
  • DataTemplate
    应用于
    ItemsControl