user3795349
user3795349

Reputation: 324

Windows 8 Phone Simplest way to populate a listbox using a list<string>

What is the Simplest way to populate a list box using a list. And how would you find the string tapped?

    List<HistoryEntry> urls = new List<HistoryEntry>();
    public HistoryEntry selectedHistory;

public MainPage()
        {
            InitializeComponent();
        }



 void Browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        // Save for fast resume
        _deactivatedURL = e.Uri;
        // We have arrived at a new page, 
        // hide the progress indicator
        _progressIndicator.IsVisible = false;
        textBox1.Text = Convert.ToString(e.Uri).Remove(0, 11);
        getHistory(textBox1.Text);
    }

private void getHistory(string url)
    {
        HistoryEntry urlObj = new HistoryEntry();
        urlObj.url = url;
        urlObj.timestamp = DateTime.Now.ToString("HH:mm yyyy-MM-dd");
        urls.Add(urlObj);

        listBox.ItemsSource = urls;
    }

    private void ListBox_SelectionChanged(object sender, GestureEventArgs e)
    {
        selectedHistory = listBox.SelectedValue as HistoryEntry;
        browserSearch(selectedHistory.url);
    }


<Grid>
                <ListBox x:Name="listBox" ItemsSource="{Binding urls}" Tap="ListBox_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>

                            <StackPanel Orientation="Horizontal">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="auto"></ColumnDefinition>
                                        <ColumnDefinition></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <StackPanel Grid.Column="1" Orientation="Vertical" Margin="0,0,0,29" >
                                        <TextBlock Text="{Binding timestamp}" FontSize="15" ></TextBlock>
                                        <TextBlock Text="{Binding url}" FontSize="25" ></TextBlock>
                                    </StackPanel>
                                </Grid>
                            </StackPanel>

                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>

Once adding new values to the list, these must be then seen in the listbox and if one is tapped the program must display which value has been clicked.

Thank you in advance :)

If you need any more details please comment and I will be happy to explain in further detail :)

Upvotes: 0

Views: 247

Answers (5)

typhoon12345
typhoon12345

Reputation: 21

Make sure to add this in the XAML

<StackPanel x:Name="stack">
        <ListBox x:Name="lst">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Str}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Content="Add new Runtime" Click="Button_Click"></Button>
    </StackPanel>

Upvotes: 1

typhoon12345
typhoon12345

Reputation: 21

Try this!

private void WebBrowser_Navigated(object sender, NavigationEventArgs e)
        {
             string url  = Convert.ToString(e.Uri).Remove(0, 11);
             HistoryEntry urlObj = new HistoryEntry();
             urlObj.URL = url;
             urlObj.timestamp = DateTime.Now.ToString("HH:mm yyyy-MM-dd");
             urls.Add(urlObj);
             listBox.ItemsSource  = null;
             listBox.ItemsSource = urls;
        }

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222582

  List<HistoryEntry> urls = new List<HistoryEntry>();
        public MainPage()
        {
            InitializeComponent();
        }

        private void WebBrowser_Navigated(object sender, NavigationEventArgs e)
        {
             string url  = Convert.ToString(e.Uri).Remove(0, 11);
             HistoryEntry urlObj = new HistoryEntry();
             urlObj.URL = url;
             urlObj.timestamp = DateTime.Now.ToString("HH:mm yyyy-MM-dd");
             urls.Add(urlObj);
             listBox.ItemsSource  = null;
             listBox.ItemsSource = urls;
        }

        public  class HistoryEntry
        {
            public string URL { get; set; }
            public string timestamp { get; set; }

        }

Upvotes: 2

Amit Bhatiya
Amit Bhatiya

Reputation: 2621

Try this:

XAML:

    <StackPanel x:Name="stack">
        <ListBox x:Name="lst">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Str}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Content="Add new Runtime" Click="Button_Click"></Button>
    </StackPanel>

CS:

    Define Global `ObservableCollection<Data> obj;`

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        obj = new ObservableCollection<Data>();
        obj.Add(new Data("string1"));
        obj.Add(new Data("string2"));
        obj.Add(new Data("string3"));
        obj.Add(new Data("string4"));
        lst.ItemsSource = obj;
    }

    //For adding new values to the list, must be seen in the listbox
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        obj.Add(new Data("New Item"));
    }

public class Data
        {
            public string Str { get; set; }

            public Data() { }

            public Data(string Str)
            {
                this.Str = Str;
            }
        }

Upvotes: 0

djack109
djack109

Reputation: 1377

do it in code, if you dont want to create objects

List<string> myList = new List<string>();

//fill your list here

foreach (string sItem in myList)
{
    ListBox1.Items.Add(sItem)
}

Upvotes: 0

Related Questions