【问题标题】:Add multi-column data to WPF ListView in VB.NET 2008在 VB.NET 2008 中向 WPF ListView 添加多列数据
【发布时间】:2011-08-16 06:13:59
【问题描述】:

我一直在搜索和搜索,但我一直无法找到一种将多列数据添加到 WPF VB.NET ListView 中的中途方法。我添加的数据不是来自数据源。

我想在搜索文件时向 ListView 添加文件和日期。

这是 ListView 和添加到其中的 GridView 的 XAML:

<Grid>
    <ListView Margin="0,101,0,0" 
              Name="dataListView">
        <ListView.View>
        <GridView x:Name="myGridView">
            <GridViewColumn Width="225" 
                                Header="File Name" 
                                DisplayMemberBinding="{Binding theName}"/>
            <GridViewColumn Width="165" 
                                Header="Date/Time"
                                DisplayMemberBinding="{Binding theDay}"/>
        </GridView>
      </ListView.View>
    </ListView>

这是我试图添加到 ListView 的代码隐藏:

Private Sub searchPath_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles searchPath.Click
    Dim dirInfo As New DirectoryInfo(sourcePath.Text)
    Dim theName, theDate As String

    dataListView.Items.Clear()
    For Each filInfo In dirInfo.GetFiles("*.QBB", SearchOption.AllDirectories)
        dataListView.Items.Add(new {theName = filInfo.Name, theDate = filInfo.LastWriteTime})
    Next
End Sub

请帮助我在 WPF VB.NET 2008 中填充此 ListView。

谢谢!

【问题讨论】:

  • 什么问题,怎么不工作?
  • 它不允许我将数据添加到多个列。我可以添加到主列,但数据不会添加到第二列。

标签: wpf vb.net .net-3.5 wpfdatagrid


【解决方案1】:

您会觉得很傻,但代码隐藏中的第二列称为 theDate,但 XAML 中的第二列绑定到 theDay。欢迎来到运行时数据绑定的危险。以后看看调试版本的输出窗口是否有绑定错误。

编辑:

这实际上是一个变相的不同问题:Visual Basic 中匿名类型的语法是什么?

例如:

Dim product = New With {Key .Name = "paperclips", .Price = 1.29}

【讨论】:

  • 感谢代码审查,但我仍然在代码隐藏中的 'DataListView.Items.Add(new {...' 行。它正在寻找一个 'With ' 在那里?没有它就无法编译。
  • 语法是New With { Key .theName = ... 见:msdn.microsoft.com/en-us/library/bb384767.aspx
  • 做到了!谢谢你,它快把我逼疯了!特别是因为我以前没有使用过 WPF。感谢您提供信息和链接!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-16
  • 1970-01-01
  • 2011-02-04
  • 2023-03-25
  • 1970-01-01
相关资源
最近更新 更多