Multidimensional databinding? How to?
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" 开发者_如何学CText="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
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):
You still did not specify where your three different blocks come from etc. but anything beyond this is definitely your problem...
精彩评论