Reputation: 41
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 });
}
});
}
XAML file
<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>
ResultsListItem class
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.
Upvotes: 0
Views: 619
Reputation: 169200
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.
Upvotes: 0
Reputation: 11
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
Upvotes: 0
Reputation: 222582
You are missing to set the ItemsSource to the Listview,try this
Search_ResultsList.ItemsSource = resultsListItems;
Upvotes: 1