C#将项目添加到listview无法正常工作

时间:2017-04-01 14:30:51

标签: c# wpf listview

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文件

<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 ResultsListItem : ListViewItem
{
    public string Index { get; set; }
    public string Result { get; set; }
    public string HyperLink { get; set; }
}

我试图通过将ResultsListItem添加到项集合来将此列表显示到ListView对象中。当我运行程序时,列表不显示listItems的内容,只显示空项目列表。我做错了什么? 提前谢谢。

3 个答案:

答案 0 :(得分:1)

您缺少将ItemsSource设置为Listview,请尝试此

<rewrite>
      <rules>            
        <rule name="About" stopProcessing="true">
          <match url="^about$"/>
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
          </conditions>
          <action type="Rewrite" url="about.asp"/>
        </rule>
        <rule name="Contact" stopProcessing="true">
          <match url="^contact$"/>
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
          </conditions>
          <action type="Rewrite" url="contact.asp"/>
        </rule>            
      </rules>
    </rewrite>

答案 1 :(得分:0)

如果要在视图中显示表格数据,可以使用DataGrid -

代码背后

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
}

请参阅 - DataGrid

的详细信息

答案 2 :(得分:0)

正如您已经注意到您的ResultsListItem应该继承自ListViewItem

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

这样做的原因是,为ListViewItem Items的{​​{1}} / ItemsSource集合中的每个项目自动生成了一个隐式ListView容器。 1}}。

如果您向ListViewItem / ListViewItem添加Items,则不会生成任何容器,这就是您在ItemsSource中看不到任何值的原因。

此外,数据对象从UI类型继承没有任何意义。