如何根据用户的选择显示特定数量的文本框?

时间:2014-11-21 21:52:52

标签: c# xaml windows-phone-8.1

用户可以选择1-8名玩家,在下一页我希望他为每个玩家输入名字。我如何根据他们的选择制作文本框。 例如,他们选择4,我想显示四个文本框,以便他们可以放四个名字。

1 个答案:

答案 0 :(得分:1)

您将变量(玩家数量)传递到下一页。获取变量后,只需将很多项添加到集合中。然后显示你的收藏。

<强> Page1.xaml.cs

// then you navigate like this (From Page1)
int number_of_players = 4;
Frame.Navigate(typeof(Page2), number_of_players);

<强> Page2.xaml.cs

int number_of_players = 0;

// and in target Page you retrive the information:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    // get the number of players passed
    number_of_players = e.Parameter as int;   
}    


// add in the correct number of players into the observable collection
private void Page_Loaded(object sender, RoutedEventArgs e)
{
    ObservableCollection<sample_model> my_list = new ObservableCollection<sample_model>();
    for (int i = 0; i < number_of_players; i++)
    {
        // where sample_model is a model of a player
        my_list.Add(new sample_model("player name"));
    }

    this.myListView.ItemsSource = my_list;
}

<ListView x:Name="myListView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="1" BorderBrush="Red">
                <TextBlock Text="{Binding PlayerName}" Width="200" Height="200"></TextBlock>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>