簡體   English   中英

具有某種網格的ListBox布局

[英]ListBox layout with some kind of Grid

我有來自XML的數據:

var searched = from c in xml.Descendants("tbody").Descendants("tr")
         let team = c.Element("td").ElementsAfterSelf("td")
         select new Time
            {
                a = c.Element("td").ElementsAfterSelf("td").First().Value,
                b = Int32.Parse(c.Descendants("td").ElementAt(3).Value),
                c = Int32.Parse(c.Descendants("td").ElementAt(4).Value),
                d = Int32.Parse(c.Descendants("td").ElementAt(5).Value),
                e = Int32.Parse(c.Descendants("td").ElementAt(6).Value),
                f = Int32.Parse(c.Descendants("td").ElementAt(7).Value),
                g = Int32.Parse(c.Descendants("td").ElementAt(8).Value),
                h = Int32.Parse(c.Descendants("td").ElementAt(9).Value),
                i = Int32.Parse(c.Descendants("td").ElementAt(10).Value),
                j = float.Parse(c.Descendants("td").ElementAt(11).Value)
            };

完成此操作后,我將它們顯示在ListBox

foreach (var item in searched)
    {
         listBox1.Items.Add(item.a + "  " + item.b + "  " + item.c + "  " + 
           item.d + "  " + item.e + "  " + item.f  + "  " + item.g + "  " + 
           item.h + "  " + item.i + "  " + item.j);
         listBox1.Items.Add("  ");
    }

打印效果很好,這就是我想要的。 現在,我需要格式化它。 現在,它是這樣打印的:

a     b    c    d  e  f  g  h  j


但是,變量的內容大小不同。 因此,信息不會變得井井有條。 所以我想要這樣的東西:

a | b | c | d | e | f | g | h | j
a | b | c | d | e | f | g | h | j

其中| 代表一列。 我當時在考慮列表框內的網格,但后來卻迷失了如何做。

那么,帶有為您的數據量身定制的ItemTemplate呢?

這是ListBox

<ListBox HorizontalAlignment="Left" Margin="12,6,0,0" Name="listBox1" VerticalAlignment="Top">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="150" />
                    <ColumnDefinition Width="150" />
                    <ColumnDefinition Width="150" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding a}" Margin="0,0,12,0" />
                <TextBlock Grid.Column="1" Text="{Binding b}" Margin="0,0,12,0" />
                <TextBlock Grid.Column="2" Text="{Binding c}" Margin="0,0,12,0" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

它為每個項目使用一個網格,將值放在單獨的列中。 您可以嘗試使用列大小。

這說明了綁定:

private void button1_Click(object sender, RoutedEventArgs e)
{
    List<MyObject> list = new List<MyObject>();
    list.Add(new MyObject() { a = 1, b = 2, c = 3 });
    list.Add(new MyObject() { a = 4, b = 57346, c = 6 });
    list.Add(new MyObject() { a = 7, b = 8, c = 9 });

    listBox1.ItemsSource = list;
}

我只是用ItemsSource數據創建一個列表,並將其設置為列表框的ItemsSource 在您的情況下,數據將來自XML。

我使用這個模擬類進行測試:

public class MyObject
{
    public int a { get; set; }
    public int b { get; set; }
    public int c { get; set; }
}

它只有3個字段來演示其工作方式。 您也可以輕松添加其他字段。 對於每個其他字段,在XAML中添加一個附加的ColumnDefinitionTextBlock ,並相應地設置Binding

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM