简体   繁体   中英

C# add items to listview not working

private void showResultsList()
{
    IWebElement table = resultFinder.FindElementById("records");
    IList<IWebElement> trElements = table.FindElements(By.TagName("tr"));
    IList<IWebElement> tdElements;
    IList<IWebElement> aElements = table.FindElements(By.TagName("a"));

    mainWindow.Dispatcher.Invoke(() =>
    {
        ListView listView = (ListView)mainWindow.FindName("Search_ResultsList");
        ItemCollection resultsListItems = listView.Items;
        listView.Items.Clear();


        for (int i = 0; i < trElements.Count; i++)
        {
            IWebElement trElement = trElements[i];
            tdElements = trElement.FindElements(By.TagName("td"));
            aElements = trElement.FindElements(By.TagName("a"));
            string index = (i + 1).ToString();
            string text = tdElements[2].Text;
            // this line removes the last part of the content, which says "Aggiungi ai preferiti"
            string result = text.Substring(0, text.LastIndexOf('\n'));
            string hyperLink = aElements[0].GetAttribute("href");

            resultsListItems.Add(new ResultsListItem { Index = index, Result = result, HyperLink = hyperLink });
        }
    });
}

<ListView Name="Search_ResultsList" HorizontalAlignment="Left" Height="886" Margin="10,10,0,0" VerticalAlignment="Top" Width="440" BorderBrush="#FF707070">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="N." Width="30" DisplayMemberBinding="{Binding Index}"/>
            <GridViewColumn Header="Result" Width="387" DisplayMemberBinding="{Binding Result}"/>
        </GridView>
    </ListView.View>
</ListView>

class ResultsListItem : ListViewItem
{
    public string Index { get; set; }
    public string Result { get; set; }
    public string HyperLink { get; set; }
}

I'm trying to show this list into a ListView object by adding the ResultsListItem to the collection of items. When I run the program the list doesn't show the content of the listItems, just a list of empty items. What do I do wrong? Thanks in advance.

您缺少将ItemsSource设置为Listview的方法,请尝试此操作

 Search_ResultsList.ItemsSource = resultsListItems;    

You can use DataGrid instead, if you want to show tabular data in the view -

Code Behind

private void showResultsListWithDataGrid()
  {
    List<ResultsListItem> listItem = new List<ResultsListItem>();
    for(int i=1; i<=10; i++)
    {
        listItem.Add(new ResultsListItem
        {
            Index = "index" + i,
            Result = "result" + i,
            HyperLink = "hyperlink" + i
        });
    }
    this.dataGrid.ItemsSource = listItem; //datagrid should be declared on the xaml file
}

please see details on - DataGrid

As you have already noticed your ResultsListItem should not inherit from ListViewItem :

public class ResultsListItem
{
    public string Index { get; set; }
    public string Result { get; set; }
    public string HyperLink { get; set; }
}

The reason for this is that an implicit ListViewItem container is automatically generated for each item in the Items / ItemsSource collection of the ListView that is not a ListViewItem .

If you add a ListViewItem to the Items / ItemsSource , no container is generated and that's why you don't see any values in your ListView .

Also, it doesn't make any sense for a data object to inherit from a UI type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM