多维数据绑定?怎样做?

发布于 2024-10-14 20:57:44 字数 4176 浏览 4 评论 0原文

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

它应该是这样的: http://tinypic.com/r/256gpxf/7

“出发地”显示在“其他站点”应该是动态的。我知道如何使用数据绑定创建标题(“其他站点”),但我需要获取每个站点的出发时间。它就像某种数据绑定中的数据绑定。

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

编辑:

公交车站的类别:

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>();
        }
    }

出发类别:

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;
        }
    }

每个车站都有一组不同的出发地点它。这就是我试图在网格中填充的内容。每个站点一个网格。这是有 4 个出发点的“静态”示例 xaml:

<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>

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

提前致谢!

/R

Im trying to create a set of grids and add them to a scrollview.

Here is what it should look like:
http://tinypic.com/r/256gpxf/7

The "depatures" showing up under "other stop" should be dynamic. I know how to create the titles ("other stop") using databinding but i need to get the depatures for each stop. It's like a databinding in a databinding of some kind.

It's hard to explain but if you look at the screenshot i think you guys can figure out what i mean :)

EDIT:

The class for 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;
        }
    }

Each stop has a diffrent set of depatures attached to it. That's what im trying to populate in a grid. One grid for each stop. Here is "static" sample xaml for a stop with 4 depatures:

<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>

Is using a grid even a good idea? It seems like i have to define each row with rowdefinitions etc.

Thanks in advance!

/R

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热血少△年 2024-10-21 20:57:44

编辑:既然您发布了代码,我就调整了模板。顺便说一句,您拼错了“Departures”,缺少“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 包含标题和另一个 ItemsControl

    <ItemsControl ItemsSource="{Binding Data}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="38" />
                            <ColumnDefinition Width="280" />
                            <ColumnDefinition Width="46" />
                            <ColumnDefinition Width="46" />
                        </Grid.ColumnDefinitions>
                        <Grid.Children>
                            <TextBlock Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Name}" FontSize="32" Foreground="#FFE37306"/>
                            <TextBlock Grid.Column="2" VerticalAlignment="Bottom" FontSize="12" Text="avgår"/>
                            <TextBlock Grid.Column="3" VerticalAlignment="Bottom" FontSize="12" Text="nästa"/>
                        </Grid.Children>
                    </Grid>
                    <ItemsControl ItemsSource="{Binding Depatures}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="38" />
                                        <ColumnDefinition Width="280" />
                                        <ColumnDefinition Width="46" />
                                        <ColumnDefinition Width="46" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.Children>
                                        <Grid Grid.Column="0" Style="{StaticResource VasttrafikGridLine}" Background="#0D4774">
                                            <TextBlock Grid.Column="0" Text="80" Style="{StaticResource VasttrafikTextLine}"/>
                                        </Grid>
                                        <TextBlock Grid.Column="1" Text="{Binding Destination}" Foreground="DarkBlue"/>
                                        <TextBlock Grid.Column="2" Text="{Binding Next}"     HorizontalAlignment="Left" Width="20" Foreground="DarkBlue"/>
                                        <TextBlock Grid.Column="3" Text="{Binding NextNext}" HorizontalAlignment="Left" Width="20" Foreground="DarkBlue"/>
                                    </Grid.Children>
                                </Grid>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

看起来像这样(当然它缺少您的特定样式和覆盖):
一张图片

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

Edit: Now that you posted your code i adjusted the template. You misspelled "Departures" by the way, it's missing an "r".

Sample data i use:

        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;

The general approach should be to define nested DataTemplates, here i use a ItemsControl whose ItemTemplate contains headers and another ItemsControl:

    <ItemsControl ItemsSource="{Binding Data}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="38" />
                            <ColumnDefinition Width="280" />
                            <ColumnDefinition Width="46" />
                            <ColumnDefinition Width="46" />
                        </Grid.ColumnDefinitions>
                        <Grid.Children>
                            <TextBlock Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Name}" FontSize="32" Foreground="#FFE37306"/>
                            <TextBlock Grid.Column="2" VerticalAlignment="Bottom" FontSize="12" Text="avgår"/>
                            <TextBlock Grid.Column="3" VerticalAlignment="Bottom" FontSize="12" Text="nästa"/>
                        </Grid.Children>
                    </Grid>
                    <ItemsControl ItemsSource="{Binding Depatures}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="38" />
                                        <ColumnDefinition Width="280" />
                                        <ColumnDefinition Width="46" />
                                        <ColumnDefinition Width="46" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.Children>
                                        <Grid Grid.Column="0" Style="{StaticResource VasttrafikGridLine}" Background="#0D4774">
                                            <TextBlock Grid.Column="0" Text="80" Style="{StaticResource VasttrafikTextLine}"/>
                                        </Grid>
                                        <TextBlock Grid.Column="1" Text="{Binding Destination}" Foreground="DarkBlue"/>
                                        <TextBlock Grid.Column="2" Text="{Binding Next}"     HorizontalAlignment="Left" Width="20" Foreground="DarkBlue"/>
                                        <TextBlock Grid.Column="3" Text="{Binding NextNext}" HorizontalAlignment="Left" Width="20" Foreground="DarkBlue"/>
                                    </Grid.Children>
                                </Grid>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

Looks something like this (it's lacking your specific styles and overrides of course):
A Picture

You still did not specify where your three different blocks come from etc. but anything beyond this is definitely your problem...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文