多维数据绑定?如何?

时间:2011-01-30 12:21:03

标签: c# silverlight xaml windows-phone-7

我正在尝试创建一组网格并将它们添加到滚动视图中。

以下是它应该是什么样子: http://tinypic.com/r/256gpxf/7

“其他停止”下显示的“depatures”应该是动态的。我知道如何使用数据绑定创建标题(“其他停止”),但我需要获取每个停止的描述。它就像是某种数据绑定中的数据绑定。

很难解释,但如果你看一下截图,我想你们可以弄明白我的意思了:))

修改

busstops的课程:

public class BusStop
    {
        public string Name { get; private set; }
        public string ID { get; private set; }
        public List<Depature> Depatures { get; private set; }

        public BusStop(string name, string id)
        {
            Name = name;
            ID = id;
            Depatures = new List<Depature>();
        }
    }

Depature class:

public class Depature
    {

        public string Destination { get; private set; }
        public int Next { get; private set; }
        public int NextNext { get; private set; }

        public Depature(string destination, int next, int nextNext)
        {
            Destination = destination;
            Next = next;
            NextNext = nextNext;
        }
    }

每个站点都附有一组不同的描述。这就是我试图在网格中填充的内容。每个站点一个网格。这是一个“静态”样本xaml,用于4个depature的停止:

<Grid Margin="0,0,0,12">
    <Grid.RowDefinitions>
        <RowDefinition Height="42" />
        <RowDefinition Height="28" />
        <RowDefinition Height="28" />
        <RowDefinition Height="28" />
        <RowDefinition Height="28" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="38" />
        <ColumnDefinition Width="280" />
        <ColumnDefinition Width="46" />
        <ColumnDefinition Width="46" />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Row="0" Grid.ColumnSpan="2" Grid.Column="0" FontSize="32" Text="Other Stop" Foreground="#FFE37306" />
    <TextBlock VerticalAlignment="Bottom" Grid.Row="0" Grid.Column="2" FontSize="12" Text="avgår"/>
    <TextBlock VerticalAlignment="Bottom" Grid.Row="0" Grid.Column="3" FontSize="12" Text="nästa"/>
    <Grid Grid.Row="1" Style="{StaticResource VasttrafikGridLine}" Background="#0D4774">
        <TextBlock Style="{StaticResource VasttrafikTextLine}" Text="80" />
    </Grid>
    <TextBlock Margin="6,0,12,0" Grid.Row="1" Grid.Column="1" Text="Nils Eriksson Term" />
    <TextBlock HorizontalAlignment="Left" Grid.Row="1" Grid.Column="2" Width="20" Text="5" />
    <TextBlock HorizontalAlignment="Left" Grid.Row="1" Grid.Column="3" Width="20" Text="15" />
    <Grid Grid.Row="2" Style="{StaticResource VasttrafikGridLine}" Background="#0D4774">
        <TextBlock Style="{StaticResource VasttrafikTextLine}" Text="80" />
    </Grid>
    <TextBlock Margin="6,0,12,0" Grid.Row="2" Grid.Column="1" Text="Nils Eriksson Term" />
    <TextBlock HorizontalAlignment="Left" Grid.Row="2" Grid.Column="2" Width="20" Text="5" />
    <TextBlock HorizontalAlignment="Left" Grid.Row="2" Grid.Column="3" Width="20" Text="15" />
    <Grid Grid.Row="3" Style="{StaticResource VasttrafikGridLine}" Background="#0D4774">
        <TextBlock Style="{StaticResource VasttrafikTextLine}" Text="80" />
    </Grid>
    <TextBlock Margin="6,0,12,0" Grid.Row="3" Grid.Column="1" Text="Nils Eriksson Term" />
    <TextBlock HorizontalAlignment="Left" Grid.Row="3" Grid.Column="2" Width="20" Text="5" />
    <TextBlock HorizontalAlignment="Left" Grid.Row="3" Grid.Column="3" Width="20" Text="15" />
    <Grid Grid.Row="4" Style="{StaticResource VasttrafikGridLine}" Background="#0D4774">
        <TextBlock Style="{StaticResource VasttrafikTextLine}" Text="80" />
    </Grid>
    <TextBlock Margin="6,0,12,0" Grid.Row="4" Grid.Column="1" Text="Nils Eriksson Term" />
    <TextBlock HorizontalAlignment="Left" Grid.Row="4" Grid.Column="2" Width="20" Text="5" />
    <TextBlock HorizontalAlignment="Left" Grid.Row="4" Grid.Column="3" Width="20" Text="15" />
</Grid>

使用网格是个好主意吗?似乎我必须使用rowdefinitions等定义每一行。

提前致谢!

/ R

1 个答案:

答案 0 :(得分:3)

修改:现在您发布了代码,我调整了模板。顺便说一下,你拼错了“离开”,它错过了一个“r”。

我使用的示例数据:

        List<BusStop> data = new List<BusStop>();
        BusStop busStop1 = new BusStop("Some Stop", "123");
        busStop1.Depatures.Add(new Depature("Nils Eriksson Term", 5, 15));
        busStop1.Depatures.Add(new Depature("Nils Eriksson Term", 5, 15));
        busStop1.Depatures.Add(new Depature("Nils Eriksson Term", 5, 15));
        busStop1.Depatures.Add(new Depature("Nils Eriksson Term", 5, 15));
        BusStop busStop2 = new BusStop("Other Stop", "42");
        busStop2.Depatures.Add(new Depature("Nils Eriksson Term", 5, 15));
        busStop2.Depatures.Add(new Depature("Nils Eriksson Term", 5, 15));
        busStop2.Depatures.Add(new Depature("Nils Eriksson Term", 5, 15));
        busStop2.Depatures.Add(new Depature("Nils Eriksson Term", 5, 15));
        BusStop busStop3 = new BusStop("Not A Stop", "0");
        busStop3.Depatures.Add(new Depature("Void", 5, 15));
        busStop3.Depatures.Add(new Depature("Void", 5, 15));
        busStop3.Depatures.Add(new Depature("Void", 5, 15));
        busStop3.Depatures.Add(new Depature("Void", 5, 15));
        data.Add(busStop1);
        data.Add(busStop2);
        data.Add(busStop3);
        Data = data;

一般方法应该是定义嵌套DataTemplates,在这里我使用ItemsControl包含标题而另一个ItemTemplate的{​​{1}}:

ItemsControl

看起来像这样(当然缺少你的特定风格和覆盖):
A Picture http://i54.tinypic.com/v8cwmd.png

你仍然没有指定你的三个不同的块来自哪里等等,但除此之外的任何东西绝对是你的问题......