我通过删除现有列并根据给定表添加新列来动态生成GridView。它显示了数据,但我在即时窗口中收到了大量错误。
System.Windows.Data错误:39:BindingExpression路径错误:' MainPartnerName'在' object'上找不到的属性'' DataRowView的' (的HashCode = 39346072)&#39 ;. BindingExpression:路径= MainPartnerName;的DataItem =' DataRowView的' (的HashCode = 39346072);目标元素是' TextBlock' (名称='&#39);目标属性是' Text' (键入' String')
Private Sub SetupGridView(ByVal objTable As Object)
'remove existing columns
Dim gv As GridView = lvClientsView.View
For x As Integer = gv.Columns.Count - 1 To 0 Step -1
gv.Columns.RemoveAt(x)
Next
'add new column for each column in table
For Each col As DataColumn In objTable.Columns
Dim gvCol As GridViewColumn = Nothing
gvCol = New GridViewColumn
gvCol.Header = col.ColumnName
gvCol.DisplayMemberBinding = New Binding(col.ColumnName)
gv.Columns.Add(gvCol)
Next
Dim bnd As New Binding
bnd.Source = objTable
lvClientsView.SetBinding(ListView.ItemsSourceProperty, bnd)
End Sub
我的XAML很简单:
<ListView Name="lvClientsView" DockPanel.Dock="Top" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="lvClientsViewItem_PreviewMouseLeftButtonUp" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
</GridView>
</ListView.View>
</ListView>
我动态拆解并重建视图。如何删除表和绑定,以便我可以分配不同的表和绑定?
答案 0 :(得分:0)
最后我发现我需要做四件事:
设置绑定到新表
Private Sub SetupGridView(ByVal objTable As Object)
'set binding to new blank binding with no data
lvClientsView.SetBinding(ListView.ItemsSourceProperty, New Binding)
'remove existing gridview and replace with new one
lvClientsView.View = Nothing
Dim gv As New GridView
lvClientsView.View = gv
'add new column for each column in table
For Each col As DataColumn In objTable.Columns
Dim gvCol As GridViewColumn = Nothing
gvCol = New GridViewColumn
gvCol.Header = col.ColumnName
gvCol.DisplayMemberBinding = New Binding(col.ColumnName)
gv.Columns.Add(gvCol)
Next
'set new binding to objTable
Dim bnd As Binding = New Binding
bnd.Source = objTable
lvClientsView.SetBinding(ListView.ItemsSourceProperty, bnd)
End Sub
工作得很漂亮!