【问题标题】:ListBox layout with some kind of Grid具有某种网格的列表框布局
【发布时间】:2011-11-22 21:10:30
【问题描述】:

我有这些来自 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

其中|代表一列。我正在考虑在列表框中设置一个网格,但后来我迷失了如何去做。

【问题讨论】:

    标签: c# xaml windows-phone-7 data-binding listbox


    【解决方案1】:

    那么,带有ItemTemplate 的 ListBox 会根据您的数据量身定制吗?

    这是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。在您的情况下,数据将来自 XML。

    我使用这个模拟类进行测试:

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

    它只有 3 个字段来演示它是如何工作的。您也可以轻松添加其他字段。对于每个额外的字段,在 XAML 中添加一个额外的 ColumnDefinitionTextBlock 并相应地设置 Binding

    【讨论】:

    • 完美!我真的在考虑列表框中的网格,但我错过了 ... 谢谢,现在的数据更有条理!一件事... 中的 每次我想添加列时都需要?
    • 是的,每个&lt;ColumnDefinition&gt; 都会提供一个列,然后可以通过Grid.Column = "x" 引用该列来放置控件。
    猜你喜欢
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    相关资源
    最近更新 更多