Windows 8 Phone使用列表<string> </string>填充列表框的最简单方法

时间:2014-07-21 13:27:16

标签: c# list xaml windows-phone-8 listbox

使用列表填充列表框的最简单方法是什么。你怎么能找到敲击的字符串?

    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>

将新值添加到列表后,必须在列表框中看到这些值,如果点击了一个,则程序必须显示已单击的值。

提前谢谢你:)

如果您需要更多详细信息,请发表评论,我将很乐意进一步详细解释:)

5 个答案:

答案 0 :(得分:2)

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

        }

答案 1 :(得分:1)

试试这个!

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

答案 2 :(得分:1)

确保在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>

答案 3 :(得分:0)

在代码中执行此操作,如果您不想创建对象

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

//fill your list here

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

答案 4 :(得分:0)

试试这个:

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