【问题标题】:How do I display an array in XAML / Windows Phone?如何在 XAML / Windows Phone 中显示数组?
【发布时间】:2013-11-09 23:17:59
【问题描述】:

我在 xaml.cs 文件中创建了一个数组列表,我正试图让它显示在 xaml 文件中。

这是我试图在 XAML 文件中显示的方法。

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

那么如何在 XAML 文件中显示它?

【问题讨论】:

  • 我不确定我是否正确理解了您的问题,但我认为您正在寻找的是ObservableCollection。查看如何将一个绑定ListBox的示例。

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


【解决方案1】:

如果您只想显示数据,您可以使用ItemsControl。如果你想在你的数组中选择,那么你应该选择ListBox。在这里,我将向您展示如何将数据提供给 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;
    }
}

【讨论】:

    【解决方案2】:

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

    给你。您需要创建带有列表框的视图和带有列表的视图模型,然后只需将视图模型绑定到视图,您就会在屏幕上看到您的列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多