0

I've created an array list in the xaml.cs file and I'm trying to get it to display in the xaml file.

This is the method I'm trying to get to display in the XAML file.

private string X1DublinBelfast()

{
    var x1stops = new List<string>();
    x1stops.Add("Dublin (Busáras)");
    x1stops.Add("Dublin Airport (Atrium Road, Zone 10)");
    x1stops.Add("Newry (Buscentre)");
    x1stops.Add("Banbridge (Kenlis Street)");
    x1stops.Add("Sprucefield (Shopping Centre)");
    x1stops.Add("Belfast (Europa Bus Centre)");

    var x1times = new List<string>();
    x1times.Add("01:00 (Departure)");
    x1times.Add("01:20 (P)");
    x1times.Add("02:30 (D)");
    x1times.Add("02:50 (D)");
    x1times.Add("03:10 (D)");
    x1times.Add("03:25 (Arrival)");

    //create a string displaying the stops/times and then return them!
    string stops = x1stops[0] + "\n\n\n" + x1stops[1] + "\n\n\n" + x1stops[2] 
        + "\n\n\n" + x1stops[3] + "\n\n\n" + x1stops[4] + "\n\n\n" 
        + x1stops[5];
    string x1times0300 = "\n" + x1times[0] + "\n\n\n" + x1times[1] 
        + "\n\n\n" + x1times[2] + "\n\n\n" + x1times[3] + "\n\n\n" 
        + x1times[4] + "\n\n\n" + x1times[5];

    string final0300 = stops + x1times0300;

    return final0300;
}

So how do I display this in the XAML file?

Noctis
  • 11,507
  • 3
  • 43
  • 82
Emmet McLaughlin
  • 144
  • 2
  • 3
  • 10
  • I'm not sure I understand your question correctly, but I think what you're looking for is an `ObservableCollection`. Look up examples of how to *bind* one to a `ListBox`. – Praetorian Nov 09 '13 at 23:26

2 Answers2

2

If you want to just display the data you can use ItemsControl. If you want selection within your array then you should go for ListBox. Here I'm showing you how to feed data to ItemsControl.

<ItemsControl x:Name="ic">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding stops}" FontSize="20" />
                <TextBlock Text="{Binding times}" FontSize="20" Grid.Column="1" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
public MainPage()
{
    var data = new List<DublinBelfast> 
    { 
        new DublinBelfast("Dublin (Busáras)", "01:00 (Departure)"),
        new DublinBelfast("Dublin Airport (Atrium Road, Zone 10)","01:20 (P)"),
        new DublinBelfast("Newry (Buscentre)","02:30 (D)"),
        new DublinBelfast("Banbridge (Kenlis Street)","02:50 (D)"),
        new DublinBelfast("Sprucefield (Shopping Centre)","03:10 (D)"),
        new DublinBelfast("Belfast (Europa Bus Centre)","03:25 (Arrival)")
    };

    ic.ItemsSource = data;
}


public class DublinBelfast
{
    public string stops { get; set; }
    public string times { get; set; }

    public DublinBelfast(string Stops, string Times)
    {
        stops = Stops;
        times = Times;
    }
}
Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115
0

How can I data bind a list of strings to a ListBox in WPF/WP7?

Here you are. You need to create view with listbox and viewmodel with list, then just bind viewmodel to view and you'll get your list on the screen.

Community
  • 1
  • 1
Nearga
  • 106
  • 3