【发布时间】:2015-04-26 03:02:49
【问题描述】:
我正在尝试将 List 数据绑定到 DataGrid。
到目前为止,它似乎只以一种方式工作 - 当我手动更改在显示的 DataGrid 中输入的内容时,List 会更新,但反之亦然(当我更改某些内容时,DataGrid 不会改变List)。
这是我的DataGrid:
<DataGrid x:Name="MyDataGrid" AutoGenerateColumns="false" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn x:Name="ColName" Binding="{Binding Name}" Header="Name" />
<DataGridTextColumn x:Name="ColProperty" Binding="{Binding MyProperty}" Header="My Property" />
</DataGrid.Columns>
</DataGrid>
我输入我的数据如下:
Public Class Res
Public Shared TableData As New List(Of DataItem)
End Class
Public Class DataItem
Public Property Name() As String
Public Property MyProperty() As String
End Class
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
Dim entry As New DataItem
entry.Name = "Test Name"
entry.MyProperty = "Test Property"
Res.TableData.Add(entry)
Me.MyDataGrid.ItemsSource = Res.TableData
End Sub
但是,当我尝试更改我的数据时,通过执行以下操作:
Res.TableData.Item(0).Name = "Changed"
不起作用,DataGrid中显示的值没有变化。
这是为什么?如何更新我的DataGrid?
如果有更好的方法将数据绑定到DataGrid,欢迎提出建议。
【问题讨论】:
-
您是否尝试过使用
{Binding Name, Mode=TwoWay}并在您的数据类上实现了INotifyPropertyChangedinterface? -
我试过
{Binding Name, Mode=TwoWay}并没有改变任何东西。不知道你说的后者是什么意思。你能举一些例子吗? -
您可以尝试点击我为您提供的链接吗?
标签: wpf vb.net data-binding datagrid